您好,我正在尝试从《Agile Web Development with Rails》一书中学习 RoR,但出现此错误:
undefined method `title' for nil:NilClass
<ul>
<% @cart.line_items.each do |item| %>
<li><%= item.quantity %> × <%= item.product.title %></li>
<% end %>
</ul>
app/views/carts/show.html.erb:4:in `block in _app_views_carts_show_html_erb___3275084074491266306_70111742218200'
app/views/carts/show.html.erb:3:in `_app_views_carts_show_html_erb___3275084074491266306_70111742218200'
这是产品型号:
class Product < ActiveRecord::Base
default_scope :order => 'title'
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
validates :title, :description, :image_url, :presence => true
validates :title, :uniqueness => true
validates :price, :numericality => {:greater_than_or_equal_to => 0.01}
validates :image_url, :format => {:with => %r{\.(gif|jpg|png)}i,:message => 'musí jít o adresu URL pro obrázek typu GIF, JPG, nebo PNG.'
}
private
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Existují položky')
return false
end
end
end
line_items_controller:
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
format.json { render action: 'show', status: :created, location: @line_item }
else
format.html { render action: 'new' }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
推车型号:
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
def add_product(product_id)
current_item = line_items.find_by_product_id(product_id)
if current_item
current_item.quantity = current_item.quantity.to_i + 1
else
current_item = line_items.build(:product_id => product_id)
end
current_item
end
end
我正在使用 Ruby 2.0.0p247 和 Rails 4。谢谢你的回答。