Skip to content

Instantly share code, notes, and snippets.

NoMethodError in Tickets#show
Showing app/views/messages/_messages.html.erb where line #6 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #6):
3: <th>Od:</th>
<table>
<tr>
<th>Od:</th>
<th>Wiadomosc:</th>
</tr>
<% for message in messages %>
<tr>
<td><%=h message.from %></td>
<td><%=h truncate(message.content, :length => 80) %></td>
<td><%= link_to 'Pokaz', message %></td>
<tr>
<td><%=unless ticket.category.nil?
h ticket.category.name
else
"Błędna kategoria!"
end
%></td>
<td><%=h ticket.employee_name %></td>
<td><%=h ticket.email %></td>
def index
@tickets = Ticket.find(:all)
end
ActionView::TemplateError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.name
On line #13 of app/views/tickets/index.html.erb
10:
11: <% for ticket in @tickets %>
12: <tr>
13: <td><%=h ticket.category.name %></td>
14: <td><%=h ticket.employee_name %></td>
15: <td><%=h ticket.email %></td>
context "on GET to /tickets/" do
setup do
get :index
end
should_respond_with :success
should_render_template :index
should_not_set_the_flash
end
<% for ticket in @tickets %>
<tr>
<td><%=h ticket.category.name %></td>
<td><%=h ticket.employee_name %></td>
<td><%=h ticket.email %></td>
<td><%=h ticket.order_number %></td>
<td><%= link_to 'Pokaż', ticket %></td>
<td><%= link_to 'Edytuj', edit_ticket_path(ticket) %></td>
<td><%= link_to 'Usuń', ticket, :confirm => 'Jesteś pewny?', :method => :delete %></td>
</tr>
def validate
errors.add(:category_id, "is not a valid category") if self.category.nil?
end
def create
if @ticket.save
flash[:notice] = "ticket created."
redirect_to(@ticket)
else
render :action => "new"
end
end
def validate
class Ticket < ActiveRecord::Base
# t.integer :category_id
# t.string :employee_name
# t.integer :order_number
# t.string :email
belongs_to :category
has_many :messages
validates_associated :category