3

我预计混合 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
4

2 回答 2

4

我已经通过在表单上直接使用 author_id 和 Responsible_person_id 解决了这个问题,就像这样

= f.input :author_id, collection: User.all, as: :select
= f.input :responsible_person_id, collection: User.all, as: :select 

代替

= f.input :author, collection: User.all, as: :select
= f.input :responsible_person, collection: User.all, as: :select
于 2012-09-25T17:52:04.027 回答
0

试试这个

<%= f.association :author %>
于 2012-09-25T12:22:31.967 回答