0

我制作了一个简单的应用程序,让用户可以跟踪他们的销售线索。除了注销用户的主页外,一切正常,他们收到错误消息 - nil:NilClass 的未定义方法“leads”。

线索控制器中的索引:

  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 %>

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

4

1 回答 1

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
于 2013-04-08T20:47:17.870 回答