Ruby on Rails : get route using controller, action & param

限于喜欢 提交于 2019-12-23 07:30:31

问题


I am quite new to RoR and I am looking for a way of getting a route for a given controller, action & param.

Something similar to url_for() but without the domain and protocol.

Lets say I have :

params = {"controller"=>"controller", "action"=>"edit", "project_id"=>"1"}

I need to get :

route = "/controller/edit/1"

It would be best if I do not have to manually build the route and if I don't need to split the result of url_for().

Does RoR natively support such a feature? It's probably an easy question, but I couldn't find an answer.


回答1:


This is the helper that will generate a path like you want:

edit_controller_path(project_id: 1)

You can use this Helper to generate the full path (including host) of the link:

edit_controller_url(project_id: 1)

Edit : More info available at http://guides.rubyonrails.org/routing.html#path-and-url-helpers




回答2:


You should be able to use the following

link_to "Link Content", :controller => "my_controller", :action => "my_action", :project_id => 1

This will produce an <a> with a link to your controller/action.



来源:https://stackoverflow.com/questions/20574179/ruby-on-rails-get-route-using-controller-action-param

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