Search code examples
ruby-on-railsroutesruby-on-rails-5

How to generate a path by specifying controller and action?


I want to generate a new link from my controller or helper. So I can't use the link_to method that is available in the view.

I want to generate the path by specifying the controller and action, not by using names paths. So e.g.:

{action: "index", controller: "accounts", id: "123"}

But what method do I put the above arguments into? I haven't seen a Path.new() method or anything like it.


Solution

  • url_for :controller => 'accounts', :action => 'index', :id => 123 
    

    Note: In versions of Rails prior to 5.1, url_helpers is not included in controllers by default. So you have to include it yourself:

    include Rails.application.routes.url_helpers