# Please help with a routing problem

**URL:** <https://www.ruby-forum.com/t/please-help-with-a-routing-problem/158646>\
**Category:** Rails\
**Created:** [January 26, 2009, 12:09pm UTC](https://www.ruby-forum.com/t/please-help-with-a-routing-problem/158646 "2009-01-26T12:09:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [January 26, 2009, 12:09pm UTC](https://www.ruby-forum.com/t/please-help-with-a-routing-problem/158646/1 "2009-01-26T12:09:34Z")

</div>

Hi All

I’m having some issues with the following routes problem.

The routes.rb file …  
map.resources :users  
map.connect ‘/users/view\_payments/:user\_id’, :controller =\>  
‘users’, :action =\> ‘view\_payments’

The users\_controller.rb file …  
def view\_payments  
@user = User.find[:params[:user\_id]]  
@payments = @user.payments  
end

The routes list from a rake routes …  
/users/view\_payments/:user\_id  
{:controller=\>“users”, :action=\>“view\_payments”}

The …/views/users/view\_payments.html.erb file …  
\<%= link\_to ‘View Payments’, :controller =\> ‘users’, :action =\>  
‘view\_payments’, :user\_id =\> @user.id %\>

Then the URL generated from the link\_to …  
[http://localhost:3000/users/view\_payments/2](http://localhost:3000/users/view_payments/2)

But this returns a 404 error

Any help/ideas appreciated

Cheers  
Adrian

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [January 26, 2009, 12:40pm UTC](https://www.ruby-forum.com/t/please-help-with-a-routing-problem/158646/2 "2009-01-26T12:40:06Z")

</div>

On Mon, Jan 26, 2009 at 1:08 PM, Adrian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The users\_controller.rb file …  
> \<%= link\_to ‘View Payments’, :controller =\> ‘users’, :action =\>  
> Adrian
> 
> >

Firstly have you restarted the server, this has bitten me more than a  
few  
times.  
Otherwise try putting the more specific route first in the routes file.

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [January 26, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/please-help-with-a-routing-problem/158646/3 "2009-01-26T13:43:37Z")

</div>

You need to define a member resource on the map.resources :users. For  
more information see the guide on routing on

> **[Ruby on Rails Guides](https://guides.rubyonrails.org)**
>
> Ruby on Rails Guides

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [January 26, 2009, 2:03pm UTC](https://www.ruby-forum.com/t/please-help-with-a-routing-problem/158646/4 "2009-01-26T14:03:03Z")

</div>

try  
map.resources :users, :member =\> {:view\_payments =\> :get}

should work.

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [January 26, 2009, 4:12pm UTC](https://www.ruby-forum.com/t/please-help-with-a-routing-problem/158646/5 "2009-01-26T16:12:25Z")

</div>

On Jan 26, 5:08 am, Adrian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def view\_payments  
> ‘view\_payments’, :user\_id =\> @user.id %\>
> 
> Then the URL generated from the link\_to …  
> [http://localhost:3000/users/view\_payments/2](http://localhost:3000/users/view_payments/2)
> 
> But this returns a 404 error
> 
> Any help/ideas appreciated
> 
> Cheers  
> Adrian

As others have said, you just need to define the custom action in your  
routes.

But I might suggest a more restful approach:

map.resources :users, :has\_many =\> :payments

Then you can use links like

[http://localhost:3000/users/2/payments](http://localhost:3000/users/2/payments)

This calls the index action of a Payments controller. You can look at  
params[:user\_id] to know which user is involved. This way you don’t  
need any custom actions.

Just a thought.

Jeff  
[switchingtorails.com](http://switchingtorails.com)

---

<div class="post-metadata">

**Author:** ![adrian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/82dd89/32.png) [@adrian](https://www.ruby-forum.com/u/adrian)\
**Post date:** [January 26, 2009, 11:14pm UTC](https://www.ruby-forum.com/t/please-help-with-a-routing-problem/158646/6 "2009-01-26T23:14:28Z")

</div>

Thanks to all who responded.

I decided to go with the more RESTful approach:

map.resources :users, :has\_many =\> :payments

and use the index action on the payments controller.

Cheers  
Adrian
