Reputation: 2044
I have this in routes.rb
match '/external_login' => 'admin#external_login', :as => :external_login, :via => [:get,:post]
And I want to generate a friendly url (for my spanish public) "proveedores"
so I have added in routes.rb:
get '/proveedores', to: redirect('/external_login')
To use a link like http.//...n/proveedores
but now I need to use url_for helper that builds http.//...n/proveedores. But How?
url_for (controller: "admin", action: "external_login")
don't return "/proveedores" reuturn "/external_login" instead
And if I put in routes.rb
match '/proveedores' => 'admin#external_login', :as => :external_login, :via => [:get,:post]
It crash when type in browser http:...localhost:3000/proveedores
Routing Error
No route matches [GET] "/external_login"
A solution, but I think I'm missing somethig
In routes, two lines:
match '/proveedores', to: 'admin#external_login', :as => :external_login, :via => [:get,:post]
get '/external_login', to: "admin#external_login"
And now I can do this:
2.0.0-p451 :003 > url_for(controller: "admin", action: "external_login")
=> "http://10.210.nn.nnn:3000/proveedores"
Can somebody explain, if this is correct, or maybe there is a simpler solution
Thanks
Thanks to Ashutosh Tiwari, the solution is simpler than it looks. Using the _url
helper instead of url_for(....
routes.rb:
match '/external_login', to: 'admin#external_login', :as => :external_login, :via => [:get,:post]
get '/proveedores', to: redirect('/external_login')
and using proveedores_url
i get the full url: "http://10.210.nn.nnn:3000/proveedores"
Upvotes: 2
Views: 448
Reputation: 998
Routes generates for two type of helpers for you, first one is routes_name_path
and another is routes_name_url
.
_url
helper provides you the absolute path while _path
helper provides relative path.
You can use _url
helper method instead of url_for
that will be something like admin_proveedores_url
.
Upvotes: 2