开发者

Rails Save File Checkbox

开发者 https://www.devze.com 2023-03-24 22:00 出处:网络
How can I make it so that my info only gets stored if the checkbox is checked. Here is what I have so far:

How can I make it so that my info only gets stored if the checkbox is checked. Here is what I have so far:

<% @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 %>
 开发者_JS百科   </div>

    #I WOULD 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.


In your purchaseds model add validations:

  validates_presence_of :name, :description, :price, :if => :purchased

Update

Add :reject_if option to your purchaseds parent model in

accepts_nested_attributes_for :purchaseds, :reject_if => {|attrs| !attrs[:purchased]}
0

精彩评论

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