Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialoxanaox
16,295 PointsI can not link to profile page
I was building my own web app with ruby and followed build simple app course. At the end I got really stuck by how do you link to the profile page of a current user after changing the routes and urls that match their profile name. I checked my routes and I have profiles_show_ path which I tried and it did not work. I also see in my routes get /:id(.:format) and that was the changed route that would match profile_name of the user and generate their profile by having localhost:3000/cleo where cleo is the profile name. How do I link to it from application.html.erb?
2 Answers

Brandon Barrette
20,485 PointsWell you will need to pass in the :id, in this case it will be the :profile_name.
Here's my setup:
get 'users/:id', to: 'users#show', as: :profile
Notice here I have 'users/:id' instead of ':id'. Reason for this is I want to avoid routing errors with any of my pages on my site being looked up as profile_names. If a user happens to pick a profile name "help" and you routed your help pages to "/help", then they could never get to their profile page. Of course, this is a silly profile_name, but you can't really prevent that (you'd have to forbid profile names of specific words and that just gets annoying).
In my user model
def to_param
profile_name
end
Then in my application.html.erb
link_to "My Profile", profile_path(current_user)
Notice if you pass in the current_user in total (the object), rails will automatically know the to_param was set to profile_name and you're set.
You can also do:
link_to "My Profile", profile_path(current_user.profile_name)
Nonetheless, you need to pass in the user whose profile you want to see.
oxanaox
16,295 PointsThank you, Brandon! I added into my routes get '/:id', to: 'profiles#show', as: :profile and in my application.html.erb I added <%= link_to "My Profile", profile_path(current_user.profile_name) %> and it works!!! I will take into consideration your advice about routing errors if users create a name like help.