user1609468
user1609468

Reputation: 113

No route matches error. Whats wrong with this code?

I'm newbie on rails and getting an error while try to add new method on my controller :(
I have a controller under admin path;

Admin::MyUsersController < ApplicationController
before_filter :......

def index
  redirect_to :action => :show_my_action
end

def show_my_action
  ...
  ...
end

My controller like this but not this exactly.
In my routes.rb

namespace "admin" do
  resources :my_users do
    get "show_my_action"
  end
end

When my routes.rb is like this, im getting error => No route matches {:action=>"show_my_action", :controller=>"admin/my_users"}

namespace "admin" do
  resources :my_users do
    get "show_my_action", :on => :collection
  end
end

when my routes.rb like this then no error :S
Why im getting this error. I can use first declaration for other controllers which is on root path.

Upvotes: 1

Views: 431

Answers (2)

Thanh
Thanh

Reputation: 8604

You are adding actions to RESTful actions, if you don't specify a collection, or a member, the route can't know what you want. If you define like this:

namespace "admin" do
  resources :my_users do
    get "show_my_action"
  end
end

How can routes know which route you want:

my_users/show_my_action, or my_users/:id/show_my_action

So, you need to specify it's member or collection:

namespace "admin" do
  resources :my_users do
    get "show_my_action", :on => :collection
  end
end

will have route: my_users/show_my_action, and:

namespace "admin" do
  resources :my_users do
    get "show_my_action", :on => :member
  end
en

will have route: my_users/:id/show_my_action

You can check at Adding More RESTful Actions.

Upvotes: 1

Tom L
Tom L

Reputation: 3409

You need to specify whether the action is on a member or a collection. If it's on a member then your URL is admin/my_users/:id/show_my_action. If it's on a collection then it's admin/my_users/show_my_action. Read up on it here: http://edgeguides.rubyonrails.org/routing.html

Upvotes: 1

Related Questions