问题
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