kwh941
kwh941

Reputation: 235

Update ruby object with hidden_field?

I'm trying to update an object using a button combined with a hidden field. But the code below updates the object with "Don't know" (outcome[4]). What am I doing wrong?

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

The HTML for each button is:

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

which also looks right...

Upvotes: 1

Views: 54

Answers (1)

lest
lest

Reputation: 8100

You could try creating several small forms instead of one big:

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

Upvotes: 4

Related Questions