-1

I am trying to hide the record item info of use on my table if its the use that is login at the moment. Basically I tried to use <% if !current_user.id === user.id %> then he needs to show it.

 <table class="table table-striped">
      <thead>
        <tr>
          <th scope="col">Id</th>
          <th scope="col">Name</th>
          <th scope="col">Email</th>
           <th scope="col">Action</th>
        </tr>
      </thead>
      <tbody>

      <% @users.each do |user| %>
<% if !current_user.id === user.id %>
        <tr>
          <th scope="row"><%= user.id %></th> 
          <td><%= user.name %></td>
          <td><%= user.email %></td>
          <td>
            <%= link_to 'Delete', user, method: :delete, data: { confirm: "Are you sure you want to delete user?"}, class: "btn btn-primary btn-user" %>
          </td>
        </tr>
      <% end %>
        <% end %>
      </tbody>
    </table>

But this one did not work. How can I hide the information of the user item if he is the one being login?

3 Answers3

0

you can use unless and using ==

<% @users.each do |user| %>
  <% unless current_user.id == user.id %>
    <tr>
      <th scope="row"><%= user.id %></th> 
      <td><%= user.name %></td>
      <td><%= user.email %></td>
      <td>
        <%= link_to 'Delete', user, method: :delete, data: { confirm: "Are you sure you want to delete user?"}, class: "btn btn-primary btn-user" %>
      </td>
    </tr>
  <% end %>
<% end %>
widjajayd
  • 6,090
  • 4
  • 30
  • 41
0
<% if !current_user.id === user.id %> # Here is an error. You are comparing false with integer, also using `===`

Try this one

<% if !(current_user.id == user.id) %>

or short version: unless

<% unless current_user.id == user.id %>

Check this one for understanding why error happened

Yurii Verbytskyi
  • 1,962
  • 3
  • 19
  • 27
0

If you are using Devise or an equivalent implementation you can write an unless clause like the following:

    <% unless user == current_user %>
        ...other content...
    <% end %>

It works without the ids.

Note that this will fail if the page is accessible for non-logged users (as there is no current_user to compare), so you might need to replace the clause for this one:

    <% unless user_signed_in? && user == current_user %>
        ...other content...
    <% end %>