简单嵌套表单中的隐藏字段未提交

发布于 2024-12-06 02:36:42 字数 1863 浏览 2 评论 0原文

环境:Rails 3.1.0,Ruby 1.9.2

我有投资组合模型,其中有多个职位,有一个资产。

这是头寸模型的架构:

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"}

Environment: Rails 3.1.0, Ruby 1.9.2

I have Portfolio model which has_many Positions which has_one Asset.

This is the schema for the Position model:

create_table "positions", :force => true do |t|
    t.integer  "portfolio_id"
    t.integer  "asset_id"
    t.decimal  "holding_percentage"
end

When the user creates a portfolio he/she should enter the portfolio name and then add positions by adding stock tickers. Jquery does its stuff and shows the full name of the asset and also inserts the asset_id into the hidden field.

I am using both nested_form and simple_form as follows:

<%= 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 %>

The problem is that the asset_id value in the hidden field is not being submitted. The parameters look as follows:

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"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2024-12-13 02:36:42

事实证明,问题在于我在隐藏字段的值中写入的是:

('.asset-num').html(data.id)

而不是:

('.asset-num').val(data.id)

It turns out that the problem was that I was writing in the value of the hidden field with:

('.asset-num').html(data.id)

Instead of:

('.asset-num').val(data.id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文