Can't get to an action in a RESTful controller

Folks,

I am using the restful_authentication plugin. This creates the users
controller which is declared as a resource in routes.rb as below

map.resources :users

Now in the UsersController I have created an action to resetpwd that I
am trying to get to from the login page when the user clicks “forgot
password”. However, when I do that the log file tells me that the call
to /users/resetpwd gets resolved to the “show” action of the controller
with id=“resetpwd” as below

Processing UsersController#show (for 127.0.0.1 at 2009-04-30 21:05:13)
[GET]
Parameters: {“id”=>“resetpwd”}
SQL (0.0ms) SET NAMES ‘utf8’
SQL (0.0ms) SET SQL_AUTO_IS_NULL=0

In the script/console when I list all the routes, here is a subset of
what I get

GET /users/:id/ {:controller=>“users”, :action=>“show”}
GET /users/:id.:format/ {:controller=>“users”, :action=>“show”}

That may explain why /users/resetpwd is mapping to the show action in
UsersController because “resetpwd” is being taken as the :id for the GET
action on /users. But when I run a recognize_path command as below in
script/console it seems to resolve correctly

rs.recognize_path “/users/resetpwd”
=> {:controller=>“users”, :action=>“resetpwd”}

I have two questions.

  1. Why does recognize_path resolve to action “resetpwd” while when the
    code runs it resolves to action “show” with id=“resetpwd”

  2. How can I make the code resolve to action=> “resetpwd” in controller
    => users?

Thanks for your help.
-S

I’m not familiar with recognize_path, so I can’t really speak to that.

If you want to add an route to the set that’s generated for you by
map.resources, see here:

So, in your case, you’d probably edit routes.rb and change your users
resource to:

map.resources :users, :collection => { :resetpwd => :get }

Then Rails would map ‘/users/resetpwd’ to resetpwd_users_url

Chris

Sj Tib wrote:

Folks,

I am using the restful_authentication plugin. This creates the users
controller which is declared as a resource in routes.rb as below

map.resources :users

Now in the UsersController I have created an action to resetpwd that I
am trying to get to from the login page when the user clicks “forgot
password”. However, when I do that the log file tells me that the call
to /users/resetpwd gets resolved to the “show” action of the controller
with id=“resetpwd” as below

Processing UsersController#show (for 127.0.0.1 at 2009-04-30 21:05:13)
[GET]
Parameters: {“id”=>“resetpwd”}
SQL (0.0ms) SET NAMES ‘utf8’
SQL (0.0ms) SET SQL_AUTO_IS_NULL=0

In the script/console when I list all the routes, here is a subset of
what I get

GET /users/:id/ {:controller=>“users”, :action=>“show”}
GET /users/:id.:format/ {:controller=>“users”, :action=>“show”}

That may explain why /users/resetpwd is mapping to the show action in
UsersController because “resetpwd” is being taken as the :id for the GET
action on /users. But when I run a recognize_path command as below in
script/console it seems to resolve correctly

rs.recognize_path “/users/resetpwd”
=> {:controller=>“users”, :action=>“resetpwd”}

I have two questions.

  1. Why does recognize_path resolve to action “resetpwd” while when the
    code runs it resolves to action “show” with id=“resetpwd”

  2. How can I make the code resolve to action=> “resetpwd” in controller
    => users?

Thanks for your help.
-S

That was a useful link Chris - thanks for sharing. I changed the entry
in routes.rb as you suggested to

map.resources :users, :collection => { :resetpwd => :get }

When I print out the possible routes in script/console I get the
following (relevant subset)

GET /users/resetpwd/ {:controller=>“users”,
:action=>“resetpwd”}
GET /users/resetpwd.:format/ {:controller=>“users”,
:action=>“resetpwd”}
GET /users/:id/ {:controller=>“users”,
:action=>“show”}
GET /users/:id.:format/ {:controller=>“users”,
:action=>“show”}

Clearly /users/resetpwd should now route to :controller=>“users”,
:action=>“resetpwd”

However when I go to my localhost/users/resetpwd I get a Routing error
saying
No route matches “/” with {:method=>:get}

In my dev log file I see the following

Processing UsersController#resetpwd (for 127.0.0.1 at 2009-05-01
09:45:40) [GET]
SQL (0.0ms) SET NAMES ‘utf8’
SQL (0.0ms) SET SQL_AUTO_IS_NULL=0
Redirected to /
Completed in 0ms (DB: 0) | 302 Found [http://localhost/users/resetpwd]

Processing ApplicationController#index (for 127.0.0.1 at 2009-05-01
09:45:41) [GET]

ActionController::RoutingError (No route matches “/” with
{:method=>:get}):

I notice a 302 response code which is redirecting the request. I don’t
understand why this is getting sent to ApplicationController. I had a
map.root statement in my routes.rb that I have temporarily commented out
to debug this problem which would address this routing error. But I
don’t understand why I can’t get to /users/resetpwd and instead get
redirected.

Thanks for your help/pointers.

-S

Thanks Colin. I looked into it more and found a bug. I can get to the
resetpwd action in the Users controller now. Chris’s routing change
suggestion resolved the issue.

-S

Are you sure it is not correctly going to UsersController#resetpwd then
getting redirected by the action?

2009/5/1 Sj Tib [email protected]