Forum: Ruby on Rails Please help with a routing problem

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Adrian (Guest)
on 2009-01-26 13:09
(Received via mailing list)
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

The routes list from a rake routes ...
{:controller=>"users", :action=>"view_payments"}

The .../views/users/view_payments.html.erb file ...
   <%= link_to 'View Payments', :controller => 'users', :action =>
'view_payments', :user_id => %>

Then the URL generated from the link_to ...

But this returns a 404 error

Any help/ideas appreciated

Andrew T. (Guest)
on 2009-01-26 13:40
(Received via mailing list)
On Mon, Jan 26, 2009 at 1:08 PM, Adrian <removed_email_address@domain.invalid>

> 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
Otherwise try putting the more specific route first in the routes file.

Andrew T.

"I have never let my schooling interfere with my education" - Mark Twain
Ryan B. (Guest)
on 2009-01-26 14:43
(Received via mailing list)
You need to define a member resource on the map.resources :users. For
more information see the guide on routing on
alexbo (Guest)
on 2009-01-26 15:03
(Received via mailing list)
map.resources :users, :member => {:view_payments => :get}

should work.
Jeff C. (Guest)
on 2009-01-26 17:12
(Received via mailing list)
On Jan 26, 5:08 am, Adrian <removed_email_address@domain.invalid> wrote:
>   def view_payments
> 'view_payments', :user_id => %>
> Then the URL generated from the link_to ...
>    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

But I might suggest a more restful approach:

map.resources :users, :has_many => :payments

Then you can use links like


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.

Adrian (Guest)
on 2009-01-27 00:14
(Received via mailing list)
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.

This topic is locked and can not be replied to.