Reputation: 4728
I have a Profile scaffold (and User which got created with Devise), each user has one profile, and want to show a link to the single user profile from my application.html.erb.
The problem is, this code which works in /views/profiles/index.html.erb but doesn't work in /layouts/application.html.erb
<td><%= link_to 'Show', profile %></td>
<td><%= link_to 'Edit', edit_profile_path(profile) %></td>
what am I missing ?
Upvotes: 0
Views: 987
Reputation: 43113
In your application layout the profile
variable isn't always set. In the profiles#index action I'm guessing you have code like this in your view: @profiles.each do |profile|
That's taking a collection of profiles and iterating through them, passing each profile object to the block, which in this case is rendering links.
In the case you're describing, if you want to show a link to the current user's profile from any page, you first need to see if there is a current user logged in, and then call the profile method on that current user. So:
<% if current_user %>
<%= link_to 'Edit Profile', edit_profile_path(current_user.profile) %>
<% end %>
More generally, in any case where you use path helpers that require an argument you have to make sure you're passing in a variable that is defined.
Upvotes: 1