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! While you're at it, check out some resources Treehouse students have shared here.
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 trial
Gerald Briones
786 PointsTreebook help - Linking to profile pages
I am working on the Treebook examples and now want to customize some of the nav bar. I have completed the profile pages, and am now wondering how I can link it on the nav bar. Any hints on how to get started going about that? I would like it to link to current user via a "my profile" type of link, but I'm sure I will eventually like to link to a user's status post as well, so that people can view their profile by clicking on the user's name.
I have tried a few things with the get :"/id" line in routes.rb but it has just been exploding on me :)
Any hints would be appreciated!
1 Answer
Twiz T
12,588 PointsGerald what is the output of
rake routes
? Basically, you should have something like profile_path which will map to a show action in the profile controller.
-twiz
Gerald Briones
786 PointsGerald Briones
786 PointsHi Twiz,
Thanks for the reply. When running rake routes, i do have a profile_path and profiles_show_path, but simply linking these will give me an error and won't actually link to the /username URL as I was hoping for. Hope that makes sense! I'll keep digging.
So just to reiterate I am trying to get a simple "My profile" link, to link to a unique username when a user is logged in. Thanks for helping!
Twiz T
12,588 PointsTwiz T
12,588 PointsWhat does your routes file look like? Basically, I think the problem is you need to pass the profile object to the profile_path for rails to know which profile it should route to. Can anyone else verify?For example if you had an index action for your profiles controller (listing all profiles on the treebook site) it would route like this in the view-
One the second line there you would pass in the profile object to the path name. The next hurdle here would be accessing the profile object outside of the profiles controller actions. You could try the following-
<%= link_to current_user.full_name, profile_path(current_user) %>
in your application.html.erb nav bar section.
Twiz T
12,588 PointsTwiz T
12,588 PointsBetter formatting for above
<%= link_to current_user.full_name, profile_path(current_user) %>-twiz
Gerald Briones
786 PointsGerald Briones
786 PointsThe answer was found in the "Creating the Friendship Form" video. You have the edit the user.rb file and add the following: def to_param profile_name end
And the routes.rb file should also have the following (if it didn't already): get '/:id', to: 'profiles#show', as: 'profile'
From here, the code you stated should work properly when linking on your page: <%= link_to status.user.full_name, profile_path(current_user) %> Actually I notice they use "status.user.full_name" instead of "current_user.full_name". Not sure if I can just use them interchangeably. Anyway, thanks for the help.