开发者

Nested Attributes when Viewing Multiple Models (not a form)

I am having trouble rendering a model's attributes in foreign model's view. I think it is a nested attributes problem because f.bike works but f.bike.biketype gives an undefined method error. Below is my error and code. Any help would be great. Thanks!

Error from Browser:

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: <h2>Your Cart</h2>
2:  <ul>
3:      <% @cart.line_items.each do |f| %>
4:      <li>开发者_开发百科<%= f.bike.biketype %></li>
5:      <% end %>
6:  </ul>

views/carts/show.html.erb

<h2>Your Cart</h2>
<ul>
    <% @cart.line_items.each do |f| %>
    <li><%= f.bike.biketype %></li>
    <% end %>
</ul>

cart.rb

class Cart < ActiveRecord::Base
  has_many :line_items, :dependent => :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

line_item.rb

class LineItem < 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

bike.rb

class Bike < ActiveRecord::Base

has_many :line_items

attr_accessible :name, :description, :size, :biketype, :price, :photo, :id, :address, :city, :state, :zip, :latitude, :longitude, :neighborhood 

end 

carts_controller.rb

  def show
  @cart = Cart.find(params[:id])
  end


Your error is telling you that the object f.bike is of class nil. You would have been expecting it to be an instance of class Bike. Obviously you have a line item in your view that doesn't have a bike record attached to it. To take care of the error so your view will display just add a check for blank?

li><%= f.bike.biketype unless f.bike.blank? || f.bike.biketype.blank? %></li>

As to why you have a line item with no bike? Well that's an entirely different question and there is not enough info here to answer that.

UPDATE Based on comments below Your button_to looks a little wrong. Try

<%= button_to "Rent this Bicycle!", {line_items_path(:bike_id => @bike)}, {:id => "rentthisbike"} %>

Using the curly braces ensures that Rails knows the second param is a css style not a param to be passed into the controllers action To check if your bike id is getting into the controller then check the params that are actually getting into the controller action that add the bike to the line item. You will be able to find this in your development.log file, find the post request for the action and one of the first lines in that action will list all the params. If you see the bike_id in that list then either one of 2 possible situations is occurring. 1) You are not accessing the params hash properly to get the bike id from it or 2) You are not setting the bike ID on the line item before saving the record.

You should be able to access the id using params[:bike_id] If you don't see the bike id then you need to look again at the button_to code to see why it's not passing the bike ID

End of update

Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜