简体   繁体   中英

Restrict logged in user in list

In the below code i want to Restrict display loggedin user in list of users

index

<div id="users">
<% for user in @users %>
  <div class="user">
    <p>
      <strong><%=h user.username %></strong>
      <%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post %>
      <div class="clear"></div>
    </p>
  </div>
<% end %>
</div>

Controller .............

@users = User.all
<div id="users">
<% for user in @users %>
  <% unless user.id == current_user.id # ← DO THIS :-) %>
    <div class="user">
      <p>
        <strong><%=h user.username %></strong>
        <%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post %>
        <div class="clear"></div>
      </p>
    </div>
  <% end %>
<% end %>
</div>

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