Testing against "current_user" with Devise in Rails 3

I'm just getting started with Ruby and Rails, testing Devise with Rails 3. I have a loop around a list of posts, each of which has a related user. I just want to display the edit controls for those posts that are related to the current user.

<% @posts.each do |post| %> <%= link_to "show" %> <% if current_user = post.user %> <%= link_to "edit" %> <% end %> <% end %> 

(The above is also simplified from memory, so I'm sure the syntax is not quite right, but you get the gist.)

If no user is logged in, messages are displayed as intended - there is a Show link, but not Edit . However, if I logged in at all, all Edit links are displayed, even fir messages created by another user.

I checked in the console that User.find(1) != User.find(2) , but for some reason current_user = post.user evaluates to true regardless of who is currently registered. Is this due to the fact that current_user is an assistant and not a "real" user object? How can I use current_user to access the current ACTUAL user for comparison?

Thanks,

Dan

+4
source share
1 answer

You assign, not check - use == - ie

 <% if current_user == post.user %> 
+7
source

All Articles