我预计混合 mongoid 和 simple_form 会遇到一些麻烦(我认为 - 对于其他表单构建器来说,同样的问题是实际的)。我有一个与关系有关的小表格,比如
f.input :author, collection: User.all, as: :select
当我提交没有选择作者的表单时,我看到了异常
NoMethodError in ExamplesController#update
undefined method `id' for "":String
很伤心:(正如我所见 - simple_form 提交“”(空字符串)但不是 nil 到控制器。当然 - 我可以从表单生成器验证每个参数,但我不确定这是一个好的解决方案。你能推荐我吗某物?
UPD(模型结构):
用户.rb
class User
include Mongoid::Document
include Mongoid::Timestamps
has_many :examples, :inverse_of => :author
has_many :examples, :inverse_of => :responsible_person
end
例子.rb
class Example
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
field :title, type: String
field :description, type: String
belongs_to :author, :class_name => "User"
belongs_to :responsible_person, :class_name => "User"
validates_presence_of :title, :description, :author
attr_accessible :title, :description, :author, :responsible_person
end