0
環境:Rails 3.1.0,Ruby 1.9.2簡單嵌套表單隱藏字段未提交
我有投資組合模型has_many has_one資產的頭寸。
這是位置模型架構:
create_table "positions", :force => true do |t|
t.integer "portfolio_id"
t.integer "asset_id"
t.decimal "holding_percentage"
end
當用戶創建一個投資組合,他/她應該進入投資組合的名稱,然後通過添加股票代碼添加位置。 Jquery做它的東西,並顯示資產的全名,並將asset_id插入隱藏字段。
我同時使用nested_form和simple_form如下:
<%= simple_nested_form_for @portfolio do |f| %>
<%= f.input :name, :placeholder => 'Your portfolio name' %>
<%= f.fields_for :positions do |position_form| %>
<%= text_field_tag 'asset-ticker', nil, :class => 'asset-ticker' %>
<span class="asset-name"></span>
<%= position_form.text_field :holding_percentage, :class => 'asset-perc' %>
<%= position_form.hidden_field :asset_id, :class => 'asset-num', :as => :hidden %>
<%= position_form.link_to_remove "Remove this position", :class => 'asset-rem-link' %>
<% end %>
<p><%= f.link_to_add "Add a Position", :positions, :class => 'asset-add-link' %></p>
<%= f.button :submit %>
<% end %>
的問題是,在隱藏字段的值ASSET_ID沒有被提交。參數如下所示:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hmvoGHF9GzpPsohQQ2MwhWk4FzhVVrf+IqoChHgftEs=",
"portfolio"=>{"name"=>"needhelpnow",
"positions_attributes"=>
{"new_1316730954406"=>{"holding_percentage"=>"11", "asset_id"=>"", "_destroy"=>"false"},
"new_1316730961085"=>{"holding_percentage"=>"22", "asset_id"=>"", "_destroy"=>"false"},
"new_1316730971587"=>{"holding_percentage"=>"33", "asset_id"=>"", "_destroy"=>"false"}}},
"commit"=>"Create Portfolio"}