Rails 4 Multiple duplicate forms on the same page with 1 submission

I need help and want to ask for a more complete Rails 4 answer. This question is very similar to How to submit multiple repeating forms from the same page in Rails - preferably with a single button and Submitting multiple forms in Rails .

My form seems to work, but I don't know what the controller looks like, especially the strong parameters and view. Below is a portion of 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 presented are as follows:

{"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: MetalsController class <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 simply refers to variables from the two models:

<h1><%= requested_metal.metal.humanize %> <%= @requested_metal.weight %></h4>

      

+3


source to share





All Articles