我正在尝试在 link_to block 中添加 URL 参数。
当前代码<%= link_to "Submit", :action => 'renderChart', :class => "btn", :remote => true, :params => params.merge(:limit => 5) %>
但这给了我一个错误。
它将 :class 和 :action 添加到 url 参数中,而不仅仅是 :limit。为什么?
编辑:
我从另一个链接添加其他 URL 参数,如下所示
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
因此,当用户单击其他链接时,我想向 url 参数添加限制并保留排序参数
最佳答案
使用这个
<%= link_to "Submit",{ :action => 'renderChart', :remote => true, :limit => 5, :sort => "desc"}, :class => "btn" %>
分离出html_options
:class
是一个html_option,所以最后传递它。
引用link_to 文档。
更新
根据OP在问题的编辑部分中的担忧:
I add other URL params from another link that looks like this
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
params :sort => "desc"
用于Toggle Sort Direction
链接,它们无法连接到Submit
链接。当您单击特定链接时,链接中指定的参数将添加到 params
哈希中。因此,如果您需要在单击 Submit
链接时将 :sort => "desc"
作为参数传递,请显式指定它们,如我的回答如上。
关于ruby-on-rails - link_to 中的 Rails params.merge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22718554/