注销用户看到错误消息“未定义的方法`引导'为nil:NilClass”

时间:2013-04-08 20:25:57

标签: ruby-on-rails-3

我制作了一个简单的应用,让用户可以跟踪他们的销售线索。一切正常,除了登出用户的主页,他们收到错误信息 - nil的未定义方法`lead':NilClass。

线索控制器中的索引:

  def index
    @leads = current_user.leads.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @leads }
    end
  end

主页:

<% if user_signed_in? %>
<div class="row">
    <div class="span3">
        <h2>Leads</h2>
    </div>
    <div class="pull-right">
        <%= link_to 'Add New Lead', new_lead_path, class: 'btn btn-primary' %>
    </div>
</div>
<div>
<table id="leads" class="display">
    <thead>
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Phone</th>
        <th class="hidden-tablet hidden-phone hidden-desktop-small">Website</th>
        <th class="hidden-tablet hidden-phone hidden-desktop-small">Email</th>
        <th class="hidden-tablet hidden-phone hidden-desktop-small">Notes</th>
        <th class="hidden-tablet hidden-phone hidden-desktop-small">Last Updated</th>    
      </tr>
    </thead>
    <tbody>
    <% @leads.each do |lead| %>
      <tr>
        <td><%= link_to lead.company_name, lead %></td>
        <td><%= lead.contact %></td>
        <td><%= lead.phone %></td>
        <td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.website %></td>
        <td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.email %></td>
        <td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.notes %></td>
        <td class="hidden-tablet hidden-phone hidden-desktop-small"><%= lead.updated_at.strftime("%d %b %y") %></td>    
      </tr>
    <% end %>
    </tbody>
</table>
</div>

<% else %>
<div class="hero-unit">
    <h1>Welcome to SnapLeads</h1>
    <p>
        The simplest CRM ever.
    </p>
    <p>
        <%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-primary btn-large" %>
    </p>
</div>

<% end %>

如果用户退出,为什么页面会尝试访问潜在客户方法?

1 个答案:

答案 0 :(得分:1)

因为索引操作中的代码总是被执行。您可以在控制器中使用以下内容:

  def index
    @leads = current_user ? current_user.leads.all : nil

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @leads }
    end
  end