Home NoMethodError - undefined method `status' for nil:NilClass
Reply: 2

NoMethodError - undefined method `status' for nil:NilClass

lostrennie
1#
lostrennie Published in 2018-01-02 18:31:40Z

I am trying to work through my table and pull out and list each of the status types under their own heading. When I run this I get NoMethodError for Tickets#index with the error reading undefined method `status' for nil:NilClass

index.html.erb

<h2>New<h2>
<% if @ticket.status == 'New' %>
    <% tickets.each do |ticket| %>
                    <p><%= link_to ticket_path(ticket) do %>
                        <strong><%= ticket.category %></strong>
                    <% end %></p>
                    <p><%= ticket.requestor_name %></p>
                    <p><%= ticket.requestor_lab %></p>
                    <p><%= ticket.contact %></p>
                    <p><%= ticket.requested_by %></p>
                    <%= ticket.status %>
    <% end %>
<% end %>

The code basically repeats for the other status types.

Controller:

 def index
    @tickets = Ticket.all

    if params[:filter_by]
        @tickets = Ticket.where(:category => params[:filter_by])
    else
        @tickets = Ticket.all
    end
end
jvillian
2#
jvillian Reply to 2018-01-02 18:50:32Z

You need to remove @ticket condition. As we don't see any definition for the same and use

<% if @tickets.any? %>
  <% @tickets.each do |ticket| %>
    <% if ticket.status.downcase == 'new' %>
      <p><%= link_to ticket_path(ticket) do %>
        <strong><%= ticket.category %></strong>
      <% end %></p>
      <p><%= ticket.requestor_name %></p>
      <p><%= ticket.requestor_lab %></p>
      <p><%= ticket.contact %></p>
      <p><%= ticket.requested_by %></p>
      <%= ticket.status %>
    <% end %>
  <% end %>
<% end %>
Rockwell Rice
3#
Rockwell Rice Reply to 2018-01-02 18:45:06Z

What is causing the error is that there are no @tickets being returned.

This line is causing the error

<% if @ticket.status == 'New' %>

undefined method ... for nil:NilClass means that the method is being called on a nil value, so @tickets is nil at the moment.

You can test this by adding a check for a value around the if statement.

<h2>New<h2>
 <% if @tickets.any? %><!-- check for tickets first -->
  <% if @ticket.status == 'New' %>
    <% tickets.each do |ticket| %>
     <p><%= link_to ticket_path(ticket) do %>
       <strong><%= ticket.category %></strong>
     <% end %></p>
                <p><%= ticket.requestor_name %></p>
                <p><%= ticket.requestor_lab %></p>
                <p><%= ticket.contact %></p>
                <p><%= ticket.requested_by %></p>
                <%= ticket.status %>
  <% end %>
 <% end %><!-- remember to add another end -->
<% end %>

That should eliminate the error, but it does not fix the @ticket variable being empty.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318102 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO