JohnSmith1976
JohnSmith1976

Reputation: 666

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.

Upvotes: 0

Views: 1343

Answers (1)

Sajad Rastegar
Sajad Rastegar

Reputation: 3154

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

Upvotes: 2

Related Questions