Ruby on rails Rails 3.2嵌套表单自定义外键无法保存
我有这样一个模型:Ruby on rails Rails 3.2嵌套表单自定义外键无法保存,ruby-on-rails,associations,ruby-on-rails-3.2,form-processing,Ruby On Rails,Associations,Ruby On Rails 3.2,Form Processing,我有这样一个模型: class Review < ActiveRecord::Base attr_accessible :approved, :reviewed_id, :for, :user_id, :text, :rating, :title belongs_to :business belongs_to :product end 我救不了协会。simple_form表示存在错误,但我在日志中看不到错误,并且所有其他字段都经过验证,因此我猜测关联存在问题 附带问题:我如
class Review < ActiveRecord::Base
attr_accessible :approved, :reviewed_id, :for, :user_id, :text, :rating, :title
belongs_to :business
belongs_to :product
end
我救不了协会。simple_form表示存在错误,但我在日志中看不到错误,并且所有其他字段都经过验证,因此我猜测关联存在问题
附带问题:我如何使简单表单显示所有错误?通过使用多态关联并在评论模型中删除for和review id来修复此问题。请参阅:
class Product < ActiveRecord::Base
include ApplicationHelper
belongs_to :business
belongs_to :catalog
belongs_to :category
has_many :reviews, :foreign_key => :reviewed_id
has_many :features, :dependent => :destroy
end
class Business < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :category
mount_uploader :photo, PhotoUploader
has_many :catalogs, :dependent => :destroy
has_many :products, :dependent => :destroy
has_many :branches, :dependent => :destroy
has_many :reviews, :foreign_key => :reviewed_id
end
def create
@business = Business.find(params[:business_id])
if params.has_key?('product_id')
@product = Product.find(params[:product_id])
@review = @product.reviews.build(params[:review])
if @review.save
flash[:notice] = 'Pending Review Submitted'
redirect_to business_product_path(@business, @product)
else
@form_resources = [@business, @product, @review]
respond_with(@business, @product, @review)
end
else
@review = @business.reviews.build(params[:review])
if @review.save
flash[:notice] = 'Pending Review Submitted'
redirect_to business_path(@business)
else
@form_resources = [@business, @review]
respond_with(@business, @review)
end
end
end