1

I am beginner in ruby + rails.

I have problem to get values in controler of checkbox with params[] of rails.

The Case:

I have simple form that that have checkbox and i want to sent reqular request to controller action with user checked values. The problem that params[:rating] can have few values.

My case: html user side:

<form id="ratings-form">
     <input name="rating" type="checkbox" value="G">G
     <input name="rating" type="checkbox" value="PG">PG
     <input name="rating" type="checkbox" value="PG-13">PG-13
     <input name="rating" type="checkbox" value="R">R
     <input type="submit" value="refresh">
</form>

controller action code to parse checked values: (get error 1. params[:rating] == nil or params[:rating] == string)

params[:rating].each  do |rat|
     p rat;
end

What should i change in the code to make it work?

Thanks

1 Answer 1

4

Try this HTML

<input name="rating[]" type="checkbox" value="G">G
<input name="rating[]" type="checkbox" value="PG">PG
<input name="rating[]" type="checkbox" value="PG-13">PG-13
<input name="rating[]" type="checkbox" value="R">R

Then you should have an array in params[:rating].

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.