用hidden_​​field更新ruby对象?

时间:2012-11-08 18:50:24

标签: ruby-on-rails ruby

我正在尝试使用结合隐藏字段的按钮来更新对象。但是下面的代码用“不知道”更新了对象(结果[4])。我做错了什么?

<%= form_for(@ask) do |f| %>
<% outcomes = [ "Advice", "Introduction", "Support", "Money", "Don't know" ] %>
<ul>
<% outcomes.each do |o| %>
    <li>
        <%= f.hidden_field :category, value: o %>
        <%= f.submit o, class: "btn btn-success" %>
    </li>
<% end %>
</ul>
<% end %>

每个按钮的HTML是:

<input id="ask_category" name="ask[category]" type="hidden" value="Introduction" /> #value is equal to each object in the array
<input class="btn btn-success" name="commit" type="submit" value="Introduction" />

看起来也正确......

1 个答案:

答案 0 :(得分:4)

您可以尝试创建几个小表单而不是一个大表单:

<% outcomes = [ "Advice", "Introduction", "Support", "Money", "Don't know" ] %>
<ul>
<% outcomes.each do |o| %>
  <li>
    <%= form_for(@ask) do |f| %>
      <%= f.hidden_field :category, value: o %>
      <%= f.submit o, class: "btn btn-success" %>
    <% end %>
  </li>
<% end %>
</ul>