将数组/哈希从表单参数传递到模型属性

发布于 2024-10-04 04:34:17 字数 589 浏览 0 评论 0原文

我在 Rails3 应用程序中有一个具有可访问属性 :days 的模型。

在我的表单中,我有一系列代表每一天的复选框。

我想将所有选定“天”的数组传递到我的模型中,以便在 before_save 回调中进行处理。

我的问题是,我可以使用标准表单助手在没有 ajax/js 的情况下构建参数哈希吗?如果不能,可以通过为每个复选框定义参数哈希来构建吗?

以下只是我如何实现这一点的猜测,因为我不太确定 Rails 如何处理 name 属性中的参数:

类似:

<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">

...?

如果是这样,它会将数组传递给我的模型吗?

欢迎任何关于如何在 Rails 表单中构造这些参数结构的好的教程或线程的建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文