简体   繁体   中英

Testing against the 'current_user' with Devise in Rails 3

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

您是分配而不是测试-使用==-即

<% if current_user == post.user %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM