ruby on rails 3 - Logged out users are seeing error message "undefined method `leads' for nil:NilClass" -


i made simple app lets users track sales leads. works except home page logged out users, error message - undefined method `leads' nil:nilclass.

the index in leads controller:

  def index     @leads = current_user.leads.all      respond_to |format|       format.html # index.html.erb       format.json { render json: @leads }     end   end 

the homepage:

<% 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 |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 snapleads</h1>     <p>         simplest crm ever.     </p>     <p>         <%= link_to "sign now!", new_user_registration_path, class: "btn btn-primary btn-large" %>     </p> </div>  <% end %> 

why page trying access leads method if user logged out?

because code in index action executed. use following in controller:

  def index     @leads = current_user ? current_user.leads.all : nil      respond_to |format|       format.html # index.html.erb       format.json { render json: @leads }     end   end 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -