3

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>
5
  • What is the actual problem and what is your actual question? Commented Jun 3, 2015 at 15:45
  • I created a way to dynamically add forms, and submit them individually via AJAX here stackoverflow.com/questions/29943737/… Additionally some of the other answers there may be more in line with how you are already trying to do it. Commented Jun 3, 2015 at 15:47
  • Why would you use one controller for multiple models? And what exactly is the problem you are facing right now with your code? Commented Jun 3, 2015 at 15:50
  • I do not know the syntax for the strong params (and get an undefined method `permit' error when I submit). Also, I do not know the embed the results of the search in a ERB view. Thank you and sorry for not being specific. Commented Jun 3, 2015 at 16:21
  • My initial reaction is that you're biting off a lot at once. Have you been able to get this form working for one metal? Get that working and then get fancy. I notice a few form-making errors that are prerequisites to getting this more-challenging form working right. Commented Jun 5, 2015 at 16:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.