views:

204

answers:

1

I'm just starting out with Ruby and Rails, trying out Devise with Rails 3. I've got a loop around a list of Posts, each of which has an associated user. I only want to display editing controls for those posts which are associated with the current user.

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

(The above is simplified, and from memory, so I'm sure the syntax isn't entirely right - but you get the gist.)

If no user is logged in, the posts show as intended - there's a Show link, but no Edit link. However, if I am logged in at all, all of the Edit links show up, even fir posts created by a different user.

I've verified in the console that User.find(1) != User.find(2), but for some reason the current_user = post.user evaluates to true no matter who is currently logged in. Is this to do with current_user being a helper as opposed to a "real" user object? How can I use current_user to get at the ACTUAL current user to make my comparison?

Thanks,

Dan

+6  A: 

You're assigning rather than testing - use == - i.e.

<% if current_user == post.user %>
Mr. Matt
I probably should've read my Ruby book more before skipping on to Rails. Not sure why I didn't instinctively use `==`, I'm a C# developer by day... Thanks!
Daniel I-S