Ruby on Rails: link-to using post method but parameters are in the URL

别等时光非礼了梦想. 提交于 2019-12-06 18:46:37

问题


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?


回答1:


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 says button_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 any params).

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.




回答2:


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} %>



来源:https://stackoverflow.com/questions/2353929/ruby-on-rails-link-to-using-post-method-but-parameters-are-in-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!