I need help and want to ask for a more complete Rails 4 response. This question is very similar to How to submit multiple, duplicate forms from same page in Rails - preferably with one button and Submitting multiple forms in Rails.
My form appears to be working, but I am not sure what the controller especially the strong params and view should look like. Below is the partial for my form.
<%= form_tag @metal, class: 'form-horizontal' do %>
<%= label_tag :metal %><br>
<%= select_tag "metal[][metal]", options_for_select(Metal.metals.map { |k,v| [k.humanize, k] }) %>
<%= label_tag :weight %><br>
<%= number_field_tag "metal[][weight]" %>
<%= label_tag :unit %><br>
<%= select_tag "metal[][unit]", options_for_select(Metal.units.map { |k,v| [k.humanize, k] }) %>
<!-- 2nd form same as first -->
<%= label_tag :metal %><br>
<%= select_tag "metal[][metal]", options_for_select(Metal.metals.map { |k,v| [k.humanize, k] }) %>
<%= label_tag :weight %><br>
<%= number_field_tag "metal[][weight]" %>
<%= label_tag :unit %><br>
<%= select_tag "metal[][unit]", options_for_select(Metal.units.map { |k,v| [k.humanize, k] }) %>
<%= submit_tag "Submit", class: "btn btn-primary btn-block" %>
<% end %>
The parameters that get submitted look like:
{"utf8"=>"✓",
"authenticity_token"=>"b0jT....=",
"metal"=>[{"metal"=>"10_karat_gold",
"weight"=>"4",
"unit"=>"grams"},
{"metal"=>"14_karat_gold",
"weight"=>"6",
"unit"=>"pennyweight"}],
"commit"=>"Submit"}
My current controller: class MetalsController < ApplicationController
def index
@metal = Metal.new
end
def create
array_number = 0
3.times do
@requested_metal = RequestedMetal.create!(metal_params)
@metal = Metal.calculate_metal(@requested_metal).first
array_number = array_number + 1
end
end
def show
end
private
def metal_params
params.require(:metal).permit([:metal, :unit, :weight, :price])
end
end
And the view just refers to variables from the 2 models:
<h1><%= requested_metal.metal.humanize %> <%= @requested_metal.weight %></h4>