Reputation: 4526
I have an order model with a product_id
foreign key and users can specify delivery_day
on a single form.
If a user selects Monday
and Friday
, I'd like to save two records of the order with different delivery_day
attributes but the same product_id
.
def new
@product = Product.find(params[:product])
@day = params[:day]
@order = Order.new(:user_id => current_user.id, :product_id => @product.id, :delivery_day => @day)
end
def create
@order = Order.new(order_params)
if @order.save
redirect_to @order, notice: 'Order was successfully created.'
else
render :new
end
end
<%= form_for(@order) do |f| %>
<div class="field">
<%= f.label :user_id %><br>
<%= f.number_field :user_id %>
</div>
<div class="field">
<%= f.label :product_id %><br>
<%= f.time_select :product_id %>
</div>
<div class="field">
<%= f.label :delivery_day %><br>
<%= f.check_box :delivery_day %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I looked around, but couldn't find an answer to this. Not sure if nested associations are overkill (like coccoon) because I'm just trying create multiple records of order
with different delivery_day
parameters.
Upvotes: 2
Views: 578
Reputation: 5905
Show the day names as checkboxes using a loop
<% Date::DAYNAMES.each do |day| %>
<div class="field">
<%= label_tag "<%= day %>" %><br>
<%= check_box_tag "delivary_days[]" %>
</div>
<% end %>
Now, receive the selected day names from your controller as params[:delivary_days]
, then manipulate the result accordingly.
In your controller:
params[:delivary_days].each do |day|
Order.create(other_parameters, :delivery_day => day)
end
I suggest you to go to the check_box_tag for necessary options.
Upvotes: 1
Reputation: 7366
You should take checked element from form as array of selected element. Something like this order[:delivery_day][]
.
You are using check_box
tag in your form this might cause some issue. Because there is a gotcha while you update through check_box
tag, You can read here.
So better you use check_box_tag helper as suggested in above link.
Try change your check_box code something like below:
<div class="field">
<%= hidden_feild_tag "order[delivery_day][]", nil %>
<%= ["mon", "tue", "wed", "thurs", "fri", "sat", "sun"].each do |day| %>
<%= check_box_tag "order[delivery_day][]", day, true_or_false_if_selected_before_add_condition %>
<%= label_tag day.capitalize %>
<% end %>
</div>
So you will get params selected in order[delivery_day][]
params. If it include blank remove them with ruby method.
And in your create and update method try create product S BELOW:
order[delivery_day].each do |day|
Order.create(other_params, :delivery_day => day)
end
Upvotes: 0
Reputation: 854
Update your form as
<%= f.label :delivery_day %><br>
<%= f.select(:delivery_day,options_for_select(Date::DAYNAMES), {multiple: true, data: {placeholder: "Select Days"}}) %>
Update your controller create action as:
def create
delivery_days = order_params[:delivery_day]
delivery_days.each do |day|
@order = Order.new(order_params.merge({delivery_day: day}))
render :new unless @order.save
end
redirect_to @order, notice: 'Order was successfully
end
Hope It will help you. Thanks
Upvotes: 0