views:

127

answers:

3

So I've got a Users controller, and it has (amongst others) a function called details.

The idea is that a user can go to localhost:3000/user/:user_id/details and be able to view the details of :user_id.

For example, I have a user called "tester". When I go to the uri: http://localhost:3000/users/tester/details I'd want the details function to be called up, to render the details view, and to display the information for the user tester.

But instead I get an error saying that

No action responded to tester. Actions: change_password, create, current_user, details, forgot_password, index, login_required, new, redirect_to_stored, show, and update_attributes

And I understand that to basically mean that if I wanted to access details, I should really be using

http://localhost:3000/users/details

Except that that isn't really working either... >.< That is instead bringing me to http://localhost:3000/users/details/registries (which is the default path that I'd stipulated for anybody trying to view users/:user_id, so again, that's working the way I wanted it to)

Point is: Can anybody help and tell me how I can go about getting users/:user_id/details to work the way I want it to and display the details of :user_id?

Thanks!

+2  A: 

Are you using resources? If your routes look like:

map.resources :users

You could make it:

map.resources :users, :member => { :details => :get }

That would allow GET requests for the URL /users/:id/details

More info here: http://guides.rubyonrails.com/routing.html#customizing-resources

dylanfm
@Jty.tan: This answer is the more "conventional" way of doing it and I think it should be the accepted answer.
Ryan Bigg
Ah ok.Sorry, I'm really struggling to figure out some of these things. I'm still trying to get my head wrapped around the idea of routes and the purposes of different requests.Now with regards to that above, can I assume that it means that the details function should be treated as a member of a user when requested via a get?And what's :collection then? I didn't understand the guide with regards with that either.
Jty.tan
And thanks for the replies so far, btw. :D
Jty.tan
Hey Jty.tan, you'd use `:member` like the example above where it's acting upon a particular record. `:collection` is for when you're acting upon a number of records. Let's say for example you have featured articles, you might go `map.resources :articles, :collection => { :featured => :get }` and then you'd be able to hit the URL `/articles/featured`.
dylanfm
A: 

In order to get routes like "users/:user_id/details" change following in routes.rb

map.users 'users/:user_id/details', :controller => 'users', :action=>'details'
Salil
Thanks! I'm still learning this stuff, so thanks for the patience! :D And thanks to dylanfm too!
Jty.tan
A: 

I think that your problem is with setting routes in such way, that instead of :user_id you have :login (or whatever) in url /users/tester instead of /users/34. Probably you should take a look at to_param (1st example, 2nd example, 3rd example).

If you want to have another option in routes (besides default REST routes), you can add :member => {:details => :get} if you are using map.resources (@dylanfm answer) or just map it like in @Salil answer.

klew
Oh, I've already set the `to_param` so that it gives me the :login instead of :user_id. :)
Jty.tan