开发者

Pass array/hash to model attribute from form parameters

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 arra开发者_如何学编程y 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜