Rails link_to action with URL parameters

房东的猫 提交于 2019-12-13 03:56:30

问题


If I have a route like so

get 'controller/:id/action/:param' => 'Controller#action'

How can I use link_to to create a link like <a href="/controller/12345/action/abcde">?

So far I have gotten link_to 'Link text', {:action => 'action'}

To give me <a href="/controller/12345/action">

but using link_to 'Link text', {:action => 'action', :param => 'abcde'}

will give me <a href="/controller/12345/action?param=abcde">


回答1:


You can name your route:

get 'controller/:id/action/:param' => 'Controller#action', :as => 'custom_show'

This should give you a new url helper which you can use like this:

<%= link_to "Foobar", custom_show_path(:id=>1234, :param=>'my_param') %>


来源:https://stackoverflow.com/questions/11782935/rails-link-to-action-with-url-parameters

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