我的模特:
class LineItem < ActiveRecord::Base
attr_accessible :itemable
belongs_to :itemable, polymorphic: true
belongs_to :lead
belongs_to :cart
end
class House < ActiveRecord::Base
has_many :line_items, :as => :itemable
end
class Appartment < ActiveRecord::Base
has_many :line_items, :as => :itemable
end
line_item_controller:
def create
@line_item = @cart.line_items.build item: @object
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart,
notice: 'Vakantiehuis toegevoegd in lijst.' }
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
private
def create_object
id = params[:house_id] || params[:appartment_id]
model = "House" if params[:house_id]
model = "Apartment" if params[:apartment_id]
model = model.constantize
@object = model.find(id)
end
创建新项目列表时,不会保存de table line_items(itemable_id,itemable_type)中的值。我在这做错了什么? thanks..remco
答案 0 :(得分:1)
尝试替换:
@cart.line_items.build item: @object
为:
@cart.line_items.build itemable: @object