2012-09-24 29 views
2

我期待着一些混合mongoid和simple_form的麻煩(我認爲 - 同樣的問題實際上是其他表單構建器)。我有一個小形式的關係,有點像mongoid,表單構建器和關係

f.input :author, collection: User.all, as: :select 

,當我提交表單沒有作者選擇我看到異常喜歡

NoMethodError in ExamplesController#update 
undefined method `id' for "":String 

如此悲傷:(我看到 - simple_form提交「」(空字符串),但不是零到控制器當然 - 。?我可以驗證從表單生成每個PARAM,但我不知道這是很好的解決方案,你能推薦我一些

UPD(模型結構) :

user.rb

class User 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    has_many :examples, :inverse_of => :author 
    has_many :examples, :inverse_of => :responsible_person 
end 

example.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 
+0

給人以代碼結構更新您的模型結構 – abhas

+0

:) –

回答

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 
+0

我有同樣的解決方案。直接使用ID並不是很乾淨,但它工作正常。 –

0

試試這個

<%= f.association :author %> 
+0

否 - 我需要可以設置空值,我的意思是 - 沒有作者或沒有負責人在這個例子中 –

+0

我有更新嘗試它我認爲如果您提交空白,協會將通過零。 – abhas

+0

它導致NoMethodError與消息 未定義的方法'valid_options'爲零:NilClass –