I have a view which has a “Like” button on it. Before it displays that like button it checks to see if the user has already liked it or not. This is the code for that like “button”.
<% if current_user.can_like_photo?(@photo) %>
<%= link_to "Like", :controller => "likes", :action => 'create', :method => "post", :id => @photo.id %>
<% else %>
<span class="caption">✓ Liked</span>
<% end %>
This works fine; IF there is a current_user. However, if you’re not logged it spits out an exception because there is no current_user. The exception is caught before the page loads, not when the like link is clicked; just to be clear. This is my current_user method in my application_helper:
def current_user
User.find(session[:user_id])
end
What is the best way to make this work if there is no current_user logged in?
You may also need the following if it is an ActiveRecord exception: