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
ruby-on-rails forms


source to share


No one has answered this question yet

See similar questions:

4
How to submit multiple repeating forms from the same page in Rails - preferably with a single button
2
Submitting multiple forms in Rails
1
Multiple records in one form

or similar:

768
How to disable submit buttons
711
Prevent users from submitting the form by pressing Enter.
471
Two submit buttons in one form
4
Getting a "private method require" called: params: Symbol "when trying to save my object
2
"Could not find [model] without identifier" Rails 3.2.3 "
2
Rails 4 Shape data is saved as nile
0
Rails 4.2: Form with multiple models - missing parameter or value
0
Group chat app with ActionCable gives error ActionController :: InvalidAuthenticityToken
0
Checkbox id not submitting on form submit
0
Reverses two shapes for one model



All Articles
Loading...
X
Show
Funny
Dev
Pics