だから私はこのエラーを持っています:
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