通过 Rails 中的hidden_field_tag 传递数组
我确实找到了这个问题,但没有帮助, 真的。
所以,我想通过隐藏字段标记传递一个数组。 截至目前,我的代码是:
<%= hidden_field_tag "article_ids", @articles.map(&:id) %>
这显然不起作用,因为它将 ids 作为字符串传递。
我该怎么做?
I did find this question on SO, but it didn't help, really.
So, I'd like to pass an array through a hidden field tag.
As of now my code is:
<%= hidden_field_tag "article_ids", @articles.map(&:id) %>
This obviously does not work since it passes the ids as a string.
How do i do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您好,也许有更好的解决方案,但您可以尝试
Hi maybe there is better solution but you may try
以下内容在 Rails 4.1.10 上对我有用
The following worked for me on Rails 4.1.10
您可以尝试将其解析为 json:
或者您可以只使用逗号分隔的字符串:
You could try to parse it to and from json:
Or you could just make use of comma separated string:
在 Rails 4 上,您可以这样做:
因为 Rails 会自动将“[]”附加到字段名称(使用
multiple
时),并且接收表单的控制器会将其视为值数组。On Rails 4 you can do:
As Rails will automatically append "[]" to the name of the field (when using
multiple
) and the controller that receives the form will see that as an array of values.