I have three models in my application, Order
, Product
and Items
. Within Items
I have the following code:
class Item < ActiveRecord::Base
belongs_to :product
belongs_to :order
end
I have a form where I add a new Order
, then I'm redirected to the view 'show.html.erb', where I have an ajax form to add the items, with fields to insert the products referring to these items , as follows:
<%= form_for [@order, @order.items.build], remote: true do |f| %>
And a listing of these items / products. It's in this listing that I'm having problems: I want my list to have the following code:
<% @order.items.each do |i| %>
<tr>
<td><%= i.product.name %> </td>
</tr>
<% end %>
However, a rendering error is thrown, saying that the name
method does not exist for null object, and I need to refresh the page to work. But, if I change this line to this \:
<td><%= i.product_id %> </td>
Rendering is done correctly.
Given this error, I believe the problem is directly related to the json return of my @item
object. But I have no idea how to solve it, can anyone help me?