# Can't get to an action in a RESTful controller

**URL:** <https://www.ruby-forum.com/t/cant-get-to-an-action-in-a-restful-controller/166939>\
**Category:** Rails\
**Created:** [May 1, 2009, 6:39am UTC](https://www.ruby-forum.com/t/cant-get-to-an-action-in-a-restful-controller/166939 "2009-05-01T06:39:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sktib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sktib](https://www.ruby-forum.com/u/sktib)\
**Post date:** [May 1, 2009, 6:39am UTC](https://www.ruby-forum.com/t/cant-get-to-an-action-in-a-restful-controller/166939/1 "2009-05-01T06:39:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sktib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sktib](https://www.ruby-forum.com/u/sktib)\
**Post date:** [May 1, 2009, 7:16am UTC](https://www.ruby-forum.com/t/cant-get-to-an-action-in-a-restful-controller/166939/2 "2009-05-01T07:16:03Z")

</div>

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:

> **[rails.info](http://guides.rails.info/routing.html#adding-more-restful-actions)**
>
> Ruby on Rails,Rails Tutorial,Ruby Basics,Rails 4,Bar Rails,Rail

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

---

<div class="post-metadata">

**Author:** ![sktib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sktib](https://www.ruby-forum.com/u/sktib)\
**Post date:** [May 1, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/cant-get-to-an-action-in-a-restful-controller/166939/3 "2009-05-01T18:58:14Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![sktib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sktib](https://www.ruby-forum.com/u/sktib)\
**Post date:** [May 1, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/cant-get-to-an-action-in-a-restful-controller/166939/4 "2009-05-01T23:50:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sktib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sktib](https://www.ruby-forum.com/u/sktib)\
**Post date:** [May 1, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/cant-get-to-an-action-in-a-restful-controller/166939/5 "2009-05-01T22:31:41Z")

</div>

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

2009/5/1 Sj Tib [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
