开发者

Rails Save Info if checkbox is checked

How can I make it so that my info only gets stored if the checkbox is checked

<% @extra.each do |extra| %>
  <%= f.fields_for :purchaseds do |builder| %>
    <div class="label-field">
      <%= builder.label :name, extra.name %>
      <p><%= extra.description %></p>
    </div>
    <div class="text-field">
      $<%= extra.price %>
      <%= builder.check_box :purchased %>
    </div>

    #I WO开发者_开发知识库ULD LIKE THIS TO ONLY GET SAVED IF THE CHECK BOX FOR PURCHASED IS CHECKED
    <%= builder.hidden_field :name, :value => extra.name %>
    <%= builder.hidden_field :description, :value => extra.description %>
    <%= builder.hidden_field :price, :value => extra.price %>  
  <% end %>
<% end %>

My client asked to be able to add extra services himself, and then users could be able to choose if they want to purchase them as accessories to their order. So what I did was I made a table called Extra (for extra services) and another table called Purchased. Purchased belongs to Order and is a nested attribute.


Well two ways. I will give you just the high level of it but if you would like me to expand, please let me know!

Option 1

Add a submit button to the form builder and have the checkbox data sent to the controller as well. There, check to see if it's set to true. If so, save the data. If not, don't save it.

Option 2

Add a jQuery function that, when the checkbox is set to true, it fires off an AJAX request to the controller to save the data.

It depends on how you want the user to be able to use this form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜