所以我有这个错误:
Routing Error
No route matches {:action=>"show", :controller=>"products", :list_id=>#<Product id: 1, name: "a", model: "", description: "", version: "", company: "", price: nil, image_source: nil, created_at: "2012-12-02 18:43:26", updated_at: "2012-12-02 18:43:26">}
注意 :list_id=>#... 看起来有点搞砸了。不知怎的Product
就进去了。
这是product.rb
class Product < ActiveRecord::Base
attr_accessible :company, :description, :model, :name, :price, :version
has_many :list_product_interactions
has_many :lists, :through => :list_product_interactions
validates_uniqueness_of :name, :scope => [:version]
这是list.rb
class List < ActiveRecord::Base
attr_accessible :activity, :description, :title
has_many :list_product_interactions
has_many :products, :through => :list_product_interactions, :order => "list_product_interactions.votes_up DESC"
这是 list_product_interactions.rb
class ListProductInteraction < ActiveRecord::Base
attr_accessible :list_id, :product_id, :votes_activity, :votes_down, :votes_total, :votes_up
belongs_to :list
belongs_to :product
当我创建产品的副本(名称已被占用的产品)时,会发生错误。这是发生错误的控制器:
products_controller.rb
class ProductsController < ApplicationController
def create
@list = List.find(params[:list_id])
@product = @list.products.build(params[:product])
if @list.save
raise "ok"
redirect_to list_path(@list)
else
@list.reload
params[:list_id] = @list.id
render template: "lists/show"
end
end
def show
@product = Product.find(params[:id])
end
end
我该如何调试?谢谢
更新:添加了 lists_controller.rb
class ListsController < ApplicationController
def index
@lists = List.all
end
def new
@list = List.new
end
def create
@list = List.new(params[:list])
if @list.save
redirect_to @list
else
render :new
end
end
def show
@list = List.find(params[:id])
@product = @list.products.build
nil
end
end
列表显示模板
h1
= @list.title
br
= @list.description
hr
- @list.products.each do |product|
- if !product.id.nil?
= link_to product.name, list_product_path(product), class: "product_link"
'
= product.version
'
= link_to "up:", "/lists/#{@list.id}/products/#{product.id}/vote_up", :method => :put
= product.product_up_votes(@list.id)
'
= link_to "down:", "/lists/#{@list.id}/products/#{product.id}/vote_down", :method => :put
= product.product_down_votes(@list.id)
hr
h3 Add products:
= form_for([@list, @product]) do |f|
- if @product.errors.any?
|product errors
= pluralize(@product.errors.count, "error")
ul
- @product.errors.full_messages.each do |msg|
li
= msg
br
= f.label :name
= f.text_field :name
br
= f.label :version
= f.text_field :version
br
= f.submit
hr
= link_to "All lists", lists_path