简体   繁体   English

Ruby on Rails undefined方法`admin?' 为零:NilClass

[英]Ruby on Rails undefined method `admin?' for nil:NilClass

I am doing the web to support the guest user can view the coordinator list. 我正在做web以支持来宾用户可以查看协调器列表。

if admin login and go 'user_path'. 如果管理员登录并去'user_path'。 admin can see all coordinator and able delete them. 管理员可以看到所有协调员并能够删除它们。

if the guest views all the user. 如果访客查看所有用户。 the guest can see all coordinator but cannot delete them. 访客可以看到所有协调员但不能删除它们。

_footer.html.erb _footer.html.erb

 <div class="footer-inquires"> 
    <div class="col-xs-6 col-md-3">
    <h5>General inquires</h5>
    <ul class="footer-links">
      <% User.all.each do |user| %>
        <li><%= link_to user.fname, user_path(user.id) %></li>  
      <% end %>

        <li><%= link_to "All", users_path %></li>

    </ul>  
  </div>

index.html.erb index.html.erb

<% provide(:title, 'All Coordinator | Course App') %>
<h1>All Coordinator</h1>

<%= will_paginate %>

<ul class="users">

  <%= render @users %>

</ul>

<%= will_paginate %>

_user.html.erb _user.html.erb

<li>
  <%= gravatar_for user, size: 50 %>
  <%= link_to user.fname, user %>
  <% if current_user.admin? && !current_user?(user) %>
    | <%= link_to "delete", user, method: :delete,
                                  data: { confirm: "You sure?" } %>

  <%else%>

************************
what do i do  for else ?

***********************
  <% end %>

</li>

current error: guest(havent login) user can not view all users. 当前错误:guest(hasnt login)用户无法查看所有用户。

Showing /home/ec2-user/environment/course_app/app/views/users/_user.html.erb where line #4 raised:

undefined method `admin?' for nil:NilClass


 <%= gravatar_for user, size: 50 %>
  <%= link_to user.fname, user %>
  <% if current_user.admin? && !current_user?(user) %>
    | <%= link_to "delete", user, method: :delete,
                                  data: { confirm: "You sure to delete?" } %>

enter image description here 在此输入图像描述

You can change your condition to if current_user && current_user.admin? && !current_user?(user) 您可以将条件更改为if current_user && current_user.admin? && !current_user?(user) if current_user && current_user.admin? && !current_user?(user) . if current_user && current_user.admin? && !current_user?(user)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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