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

index アクションのコードは常に実行されるためです。コントローラーで次のようなものを使用できます。

  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 に答える