abelard2008
abelard2008

Reputation: 2084

How to get "http://localhost:3000/users/current" from user_path(:current)?

I tried to practice #163 Self-Referential Association using Rails 3.2.21 and work fine almost, but I can only display

http://localhost:3000/users/1

rather than

http://localhost:3000/users/current

in URL address bar. I read and search almost all code, I only find one current:

<%= link_to "View Profile", user_path(:current) %>.

What should I do for this? It is relative to Rails 3? Thanks a lot!

Upvotes: 0

Views: 116

Answers (2)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

The Railscast is a little unconventional, but the user_path() method, will take in a value (object, string, int, symbol) and call to_param on it. A symbol (in this case :current) will turn into "current" and the url built will be "/users/current". If you pass it a @user (User instance), the to_param method will return a "1" (the id of the object) giving you "/users/1".

I say this code is unconventional because the users_controller#show method doesn't use the ID to find the @user. It just sets it to current_user for convenience. Ryan was not trying to teach this necessarily, more about the restful friendship controller and data-modeling friendships.

Upvotes: 2

Nitin Rajan
Nitin Rajan

Reputation: 307

Try this

<%= link_to "View Profile", users_path(:current) %>.

Upvotes: 0

Related Questions