1

我正在尝试使用结合隐藏字段的按钮来更新对象。但是下面的代码将对象更新为“不知道”(结果 [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" />

这看起来也不错...

4

1 回答 1

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>
于 2012-11-08T19:17:41.007 回答