私はfields_forタグを持っており、ここでプレフィックスを指定します(いくつかの正当な理由で言いましょう)。これは1対1の関係を表すことになっています。
私は関係を表現しようとしています
widget has_many thingamagigs
thingamagig has_one whatchamacallit
field_forコードは次のとおりです。
fields_for "widgt[thingamagigs_attributes][][whatchamacallit_attributes]", thingamagig.whatchamacallit do |x|
これは(間違って)名前を生成します:
widget[thingamagigs_attributes][][whatchamacallit_attributes][][value]
より良い解決策は
t.fields_for :whatchamacallit do |x|
ここで、t = fields_for thethingamagig ...ただし、これを行うと、次の名前が生成されます。
widgt[thingamagigs_attributes][whatchamacallit_attributes][]
これは、thingamagigの他のすべてのフィールドが...であるため、完全に間違っています。
widgt[thingamagigs_attributes][][name]
だから、すべての場合で私は困惑しています。文字列を使用する元のfield_foraccepts_nested_attributes_for :whatchamacallit
は、whatchamacallitが特異な関係であり、オブジェクトは配列ではないと予想されるため、一緒に使用することはできません。Railsはparamsオブジェクトを正しく解析できないため、2番目のfields_forは単純に機能しません。[whatchamacallit_attributes]
すべてのフィールド名の後に[]を追加しないように最初のforms_forに指示する方法はありますか?
私の現在の解決策は、モデルを次のように拡張することです。
def whatchamacallit_attributes=(whatchamacallit_attributes)
assign_nested_attributes_for_one_to_one_association(:whatchamacallit, whatchamacallit_attributes[0])
end
これは、壊れたフォームフィールドでも機能します。しかし、これは非常にハッキーだと感じます、誰かが解決策を持っていますか?