ハイ、
入れ子になった形で問題があります。実際には 2 つの問題がありますが、2 つ目はマイナーです。基本的な構造は次のとおりです。
class Connectable
has_one :web_account
accepts_nested_attributes_for :web_account
end
class Person < Connectable
end
class WebAccount
belongs_to :owner, class_name => `Connectable`
end
そして今、ネストされたフォームを作成して、Person と WebAccount を同時に作成したいと考えています。私は次のコードを持っています:
<%= form_for @person do |f| %>
...
<%= f.fields_for web_accounts do |child| %>
....
<end>
<end>
WebAccount の唯一の重要な属性は name であり、文字列です。
child_form の定義で使用される複数形にも注意してください。理由はわかりませんが、単数形を使用すると(私には正しいように思えます)、レールは空のフォームを出力するだけですが、複数形を使用すると問題なく機能します。コントローラーにコードを追加して、フォームで指定された :web_accounts ハッシュ エントリを :web_account に置き換えました。
さらに重要なことに、次のエラーが発生します。
WebAccount(#97097470) expected, got ActiveSupport::HashWithIndifferentAccess(#83887850)
また、次のハッシュを定義して、コンソールで実行してみました:
p = { :name => "ab", :lastname => "cd", :web_account => { :name => "ab.cd" }}
しかし、同じ結果になります。
これが私のコントローラのコードです:
def create
params[:person][:status] = Status.where(:name => params[:person][:status]).first
# Transforms the web_accounts entry into web_account
params[:person][:web_account] = params[:person][:web_accounts]
params[:person].delete(:web_accounts)
@person = Person.new(params[:person])
.... (the rest is the standard response)
end
エラーは、上記のコードの Person.new(...) 行である 58 行目に表示されます。必要に応じて完全なフレームワーク トレースを出力することもできますが、いつものようにかなり長くなります。
なぜこれが機能しないのですか?私はすべてのオンラインチュートリアルに従っているように見えるので、それについて頭を悩ませることはできません...それは継承でしょうか?