使用 Rails Button_to helper 生成隐藏表单字段数据?
目前,我使用以下代码将 params[:id] 传递给 line_items create
<%= button_to 'Add to Cart', { controller: 'line_items', action: 'create', id: brick }, class: 'green radius nice button', method: :post, remote: true %>
但是,它将在 url 查询上附加 id 参数,而我不认为 button_to 会这样做。我想要的是通过隐藏表单字段传递 id 参数。我可以在 Button_to 中执行此操作或解决此问题的任何解决方法吗?
Currently, i use the following code for passing params[:id] to line_items create
<%= button_to 'Add to Cart', { controller: 'line_items', action: 'create', id: brick }, class: 'green radius nice button', method: :post, remote: true %>
However, it will append the id param on url query which i didn't suppose button_to to do. What i want is passing the id param via hidden the form field. Can i do that in button_to or any workaround for this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Rails 4.1.0 开始, button_to 具有现在附加选项 :params 可以接收将转换为隐藏字段的哈希值。例如:
将给出:
Starting with Rails 4.1.0, button_to has now the additional options :params which can receive a hash that will be transformed into hidden fields. For example:
Will give :
我在项目的模块中做了类似的事情:
首先,我添加了一个带有隐藏字段的表单:
列表中的所有 mi 项目都会有一个 check_box_tag ,其中包含项目 id:
当选择一个项目时,只需捕获事件,替换hidden_field 上的值并触发表单:
如果您想渲染内容,只需将 action.js.erb 文件添加到控制器视图文件夹中,但那是另一回事。
I have done something similar in a module of my project:
First, I added a form with a hidden field:
all mi items on the list will have a check_box_tag wich contains the item id:
when an item is picked just catch the event, replace the value on the hidden_field and trigger the form:
if you want to render stuff just add a action.js.erb file to your controller view folder, but thats another thing.