Ruby on Rails:使用 post 方法链接,但参数位于 URL 中
我正在使用
link_to 'My link', path(:arg1 => session[:arg1], :arg2 => session[:arg2],:arg3 => anyobject.id), :method => :post
,但生成的 HTML 链接包含 (arg1,arg2,arg3) 作为 URL 查询参数。
如何去除它们?我错过了文档中的某些内容吗?
I'm using
link_to 'My link', path(:arg1 => session[:arg1], :arg2 => session[:arg2],:arg3 => anyobject.id), :method => :post
but the HTML link being generated includes (arg1,arg2,arg3) as URL query parameters.
How can remove them? Did I miss something in the documentation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
link_to
始终会将参数放入查询字符串中,因为它正在创建 get 样式的 HTML 链接 - 即使您将:method =>; :post
只是附加一个额外的(“特殊”)参数_method
。我认为你真正想要的是一个
button_to
链接 - 它将使其成为一种表单帖子。它的工作原理相同,但它显示为button_to
(例如,button_to 'My link', path(:params => :go_here)
。缺点是它看起来像一个按钮。但是您可以给它一个 CSS 类(例如“unbutton”),然后更改该 CSS 类的样式,使其看起来不像一个按钮。或者,如果您真正想要的实际上是没有 按钮。
params
完全传递给控制器...然后在创建链接时不要包含它们(例如,link_to“My link”路径
- 不需要:post
如果您不想发布任何params
)。最后,如果您想要的是
params
成为 的一部分。 URL(例如,stuff/[param_1]/more_stuff/[param_2]
等),那么您需要更新您的路由以将这些参数作为选项包含在内。 rdoc 了解如何执行此操作。A
link_to
will always put the arguments into the query string as it is creating a get-style HTML link - even if you put:method => :post
that just appends an extra ("special") argument_method
.What I think you really want is a
button_to
link - which will make it into a sort of form-post. It works the same, but it saysbutton_to
instead (for example,button_to 'My link', path(:params => :go_here)
. The downside is that it will look like a button. But you can give it a CSS class (eg "unbutton") and then change the styling on that CSS class to make it not look like a button.Alternatively, if what you really want is actually to have no
params
passed to the controller at all... then just don't include them when making your link (for example,link_to "My link" path
- there's no need for:post
if you don't want to post anyparams
).Finally, if what you want is for the
params
to become a part of the URL (for example,stuff/[param_1]/more_stuff/[param_2]
, etc.) then you need to update your routes to include these parameters as options. Have a look at the routing section of the rdoc for how to do that.您可以使用下面的代码,其中 rails.js 需要 data-method 在 Ajax 中切换到 post 模式。
<%= link_to ''.html_safe, { 控制器: :lots, id: @lot.containername , 操作: "print_#{print_template}", 远程: true }, {'data-method' => :post} %>
You can use below code, which rails.js need data-method to switch to post mode in Ajax.
<%= link_to '<button id="print" type="submit" class="btn">Print</button>'.html_safe, { controller: :lots, id: @lot.containername, action: "print_#{print_template}", remote: true }, {'data-method' => :post} %>