2016-11-09 49 views
0

我有一個一對多的關聯在我的代碼,像這樣:驗證上formtastic嵌套字段

class Second < ActiveRecord::Base 
    has_many :firsts 
end 
class First < ActiveRecord::Base 
    belongs_to :second 
    accepts_nested_attributes_for :second 
end 

在我ERB爲一,我有:

<%= f.input :one_field, :label => false %> 
<%= f.semantic_fields_for :second do |cp_f| %> 
    <%= cp_f.input :another_field, :as => :string, :label => "another field" %> 
<%= end %> 

形式正確將數據填充到嵌套表中。

我需要把一些驗證在控制器中,我想指出用戶到錯誤發生的領域。如果我寫這樣的錯誤:

errors.add :one_field, "This is wrong" 

這工作沒有問題,並把錯誤在頁面右側的字段。但我想這樣做同樣的事情嵌套的領域,如可能:

errors.add :second.another_field, "Another wrong one" 

但我得到一個錯誤:

undefined method `another_field' for :second:Symbol 

有沒有把一個錯誤的嵌套字段的方式?

回答

0

問題在於訪問First的錯誤成員。我需要的是:

second.errors.add :another_field, "Another wrong one"