11

Rails 3.2.5 でネストされたフォームを実行していますが、私の非表示を追加するとaccepts_nested_attributes_for(fields_forフォームに表示されなくなります)。
これは私のモデルです:

class Product < ActiveRecord::Base
    attr_accessible :title, :description, :variants_attributes

    has_many :variants
    accepts_nested_attributes_for :variants

    validates :title, presence: true
end  

私の2番目のモデルは

class Variant < ActiveRecord::Base
    belongs_to :product
    attr_accessible :price, :sku, :weight, :inventory_quantity, :product_id
end

そして、私の見解では、

<%= form_for [:admin, @product], :html => {:multipart => true} do |f| %>

 <%= render 'admin/shared/error_messages', object: f.object %>

 <fieldset>
  <legend>Producto Nuevo</legend>  
    <div class="control-group">
      <%= f.label :title, 'Título', class: 'control-label' %>
      <div class="controls">
        <%= f.text_field :title %>
      </div>
   </div>

    **<%= f.fields_for :variants do |variant| %>
     <%= render 'inventory_fields', f: variant %>
    <% end %>**  

  <div class="actions">
    <%= f.submit 'Guardar', class: 'btn btn-primary' %>
  </div>
<% end %>

_inventory_fields.html.erb

<div class="control-group">
  <%= f.label :inventory_quantity, 'How many are in stock?', class: 'control-label' %>
  <div class="controls">
    <%= f.text_field :inventory_quantity, class: 'span1', value: '1' %>
  </div>
</div>  

** で囲まれた部分は印刷されていない部分です。製品モデル fields_for のaccepts_nested_attributes_forを削除すると、再び表示されますが、フォームが機能しません。
何が起こっていますか?!?!

4

1 に答える 1

17

コントローラーの新しいアクション呼び出しで

@product.varients.build

これにより、製品バリアント コレクションのメモリ内に 1 つのバリアントが作成され、フィールドにバインドされるはずです。

于 2012-08-02T03:28:25.303 に答える