1
<form action="/decide_start_session" method="get">
    <input type="checkbox" name = "subject" value = "NetPresentValue" checked> Net Present Value
    <input type="checkbox" name = "subject" value = "FamaFrench" checked> Fama French
    <%= submit_tag("Start session", class: "btn btn-success") %>
</form>


puts params[:subject]

I have the first part in my view and the second line in my controller in my Rails app. If a user selects both checkboxes, the Rails console only returns the last item checked instead of all of them together. Any idea on how I can fix this (show all the checkboxes in the console)?

2 Answers 2

2

If you change the name attribute of checkboxes to subject[] and remove spaces around = in your html, then checked values will be passed to the controller as array.

<form action="/decide_start_session" method="get">
    <input type="checkbox" name="subject[]" value="NetPresentValue" checked> Net Present Value
    <input type="checkbox" name="subject[]" value="FamaFrench" checked> Fama French   
</form>

Then you can do this in your controller:

puts params[:subject].inspect
Sign up to request clarification or add additional context in comments.

Comments

0

Use name="subject[]" and then you will get an array of selected values in params[:subject]

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.