Note that there are some explanatory texts on larger screens.

plurals
  1. PONested Attributes when Viewing Multiple Models (not a form)
    text
    copied!<p>I am having trouble rendering a model's attributes in foreign model's view. I think it is a nested attributes problem because <code>f.bike</code> works but <code>f.bike.biketype</code> gives an undefined method error. Below is my error and code. Any help would be great. Thanks!</p> <p>Error from Browser:</p> <pre><code>NoMethodError in Carts#show Showing /Users/willdennis/rails_projects/spinlister/app/views/carts/show.html.erb where line #4 raised: undefined method `biketype' for nil:NilClass Extracted source (around line #4): 1: &lt;h2&gt;Your Cart&lt;/h2&gt; 2: &lt;ul&gt; 3: &lt;% @cart.line_items.each do |f| %&gt; 4: &lt;li&gt;&lt;%= f.bike.biketype %&gt;&lt;/li&gt; 5: &lt;% end %&gt; 6: &lt;/ul&gt; </code></pre> <p>views/carts/show.html.erb</p> <pre><code>&lt;h2&gt;Your Cart&lt;/h2&gt; &lt;ul&gt; &lt;% @cart.line_items.each do |f| %&gt; &lt;li&gt;&lt;%= f.bike.biketype %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; </code></pre> <p>cart.rb</p> <pre><code>class Cart &lt; ActiveRecord::Base has_many :line_items, :dependent =&gt; :destroy belongs_to :user accepts_nested_attributes_for :line_items attr_accessible :bike_id, :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood end </code></pre> <p>line_item.rb</p> <pre><code>class LineItem &lt; ActiveRecord::Base belongs_to :bike belongs_to :cart accepts_nested_attributes_for :bike, :cart attr_accessible :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood end </code></pre> <p>bike.rb</p> <pre><code>class Bike &lt; ActiveRecord::Base has_many :line_items attr_accessible :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood end </code></pre> <p>carts_controller.rb</p> <pre><code> def show @cart = Cart.find(params[:id]) end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload