0

I have a model with an accessible attribute :days in a Rails3 app.

In my form I have a series of checkboxes which represent each day.

I'd like to pass an array of all the selected 'days' into my model for processing in a before_save callback.

My question is, can I construct a parameters hash without ajax/js using either standard form helpers, or if not, by defining the parameters hash for each checkbox?

The following is just a guess on how I might implement this as I'm not quite sure how rails processes the parameters in name attribute:

Something like:

<input type="checkbox" value="1" name="my_model[days][0]" id="my_model_days">
<input type="checkbox" value="2" name="my_model[days][1]" id="my_model_days">

...?

and if so will it pass an array to my model?

Any suggestions on good tutorials or threads on how to construct these parameter structures within Rails forms are welcome.

1 Answer 1

1

Use fields_for http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

Sign up to request clarification or add additional context in comments.

1 Comment

Cool, I've only used fields_for to pass model attributes with accepts_nested_att... before, didn't know you could use it like that, cheers!

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.