0

I'm having trouble getting multiple parameters to be submitted when I use a checkbox submit. How would I send both parameters?

I have two items in each row in a table, the columns are "product revenue" and "product title" and I use a check box on each row to select it and then on submit I'd like to send both of these values to the controller. I can get one item working, but wasn't able to figure out how to send the second item as well I tried using a hidden field, but I couldn't get that working.

View Code

<%= form_tag add_multiple_path, method: :post do %>
    <%= check_box_tag 'price_test_datum[product_title][]', p.dimensions[0] %>
    <% hidden_field_tag('price_test_datum[product_price][]', p.metrics[0].values[0]) %>
    <%= submit_tag "Add selected" %>
<% end %>

Controller Code (only for one item at the moment since both params aren't being sent)

def add_multiple
params[:price_test_datum][:product_title].each {|p| PriceTestDatum.create(product_title: p) }
 respond_to do |format|
  format.html { redirect_to price_test_data_path }
  format.json { head :no_content }
 end
end

Parameters sent: {"utf8"=>"✓", "authenticity_token"=>"token here", "price_test_datum"=> {"product_title"=>["Widget 1", "Widget 2"]}, "commit"=>"Add selected"}

5
  • Check out that <% before your hidden_field_tag. It seems lonely for an =. Commented Jun 30, 2017 at 20:28
  • Thanks for the comment. When I use the <%= the hidden field shows up in params sent, but for some reason it dumps all the values in the column for product revenue? Commented Jun 30, 2017 at 20:32
  • That's really a separate question, isn't it? Accept this one and ask a new one and I'll take a look at it. Commented Jun 30, 2017 at 20:44
  • I accepted your answer, I'll have to wait about an hour before I can post another question however. Thanks Commented Jun 30, 2017 at 20:48
  • Sure thing. Also, when you post, please provide detail about what's in your p variable (where you say p.metrics[0].values[0]). Commented Jun 30, 2017 at 20:56

1 Answer 1

0

To your original question (before your correction), you're missing an = here:

<% hidden_field_tag('price_test_datum[product_price][]', p.metrics[0].values[0]) %>

It should be:

<%= hidden_field_tag('price_test_datum[product_price][]', p.metrics[0].values[0]) %>
Sign up to request clarification or add additional context in comments.

Comments

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.