rake routes gives me a mapping of "controllerName#method" to URL path format
Rails.application.routes.named_routes.helpers gives me a list of all helper methods.
Is there something that can give me a mapping of all helper methods to URL path formats? I'm not very experienced with Rails so something that tells me what URL the Rails helper methods actually generate would be EXTREMELEY awesome.
I'm confused what you're asking? I think rake routes already tells you what you're looking for. Entries are in the format:
route_name HTTP_VERB path controller#action
Appending _path or _url to the route_name will give you a url helper. Ex. in my app I have
scenario GET /scenarios/:id(.:format) scenarios#show
And I can get a path or url to it by using scenario_path(scenario) or scenario_url(scenario) respectively.
Related
What is the difference between <route>_path and <route>_url in the Ruby on Rails?
For example, we use route_path and route_url in the route.rb file of the rails.
*_path returns just a path part of the url, e.g. /documents/1, *_url gives you full URL with protocol and host https://myapp.com/documents/1
In Django template, I can use
{% url 'pathname' p1 %}
to represent a defined path in url.py. e.g. above will show '/path/to/method/p1'. Is there any method in Rails view (in .html.erb file) to do the same thing?
The "link_to" tag is not useful because it also generate the "<A>" html tag but I only need to get the path of url.
Thanks,
Have you gone through rails routing guide ? if not pls go through as it covers routing very well.
Coming to your question, rails has url helpers.
So for example if your routes file has this route get '/patients/:id', to: 'patients#show' then you could access this url like patient_path(#patient).
You could also define its name like
get '/patients/:id', to: 'patients#show', as: 'patient' where as: patient is the user defined name that could be used in the views to access this url like patient_path. You append _path to get the relative url.
In rails, when I have a form with a path-helper, for instance create_admin_shop_voucher_path and I want to find out what controller action it points to, I look in routes.rb:
rake routes | grep 'create_admin_shop_voucher'
But how do I do the same for a url-helper, for instance passed_url ?
Lets say you have this route in your RAILS_ROOT/config/routes.rb file
get "passed" => "passed#index", :as => :passed
If you call passed_url
rails will return the whole url. For instance http://localhost:3000/passed
if you call passed_path
rails will return the relative path /passed
*_url helper generates a URL that includes the protocol and host
name. The *_path helper generates only the path portion.
So, the its the same route in your RAILS_ROOT/config/routes.rb file
In your case you can call create_admin_shop_voucher_url as well as create_admin_shop_voucher_path.
Mostly you should use the _path flavor. If you need to spec the host or
protocol (like for talking to another app or service), then use the _url
flavor.
I use _url when i want to pass for instance a subdomain to a link.
Can't understand how this method isn't found?
undefined method `change_password_admin_user' for #<ActiveAdmin::Views::ActionItems:0x007ff1cad9d848>
Output of my rake routes
change_password_admin_user GET /admin/users/:id/change_password(.:format) /users#change_password
Appears that you don't have used the suffix in the method call. Two methods are generated by the route that you pasted in in your question:
change_password_admin_user_path
Which return a path relative to the domain, and the other version which includes the host of your site:
change_password_admin_user_url
I know that the file routes.rb in a Rails application maps a URL to a controller, a method, and a set of parameters in the params hash. This is complicated somewhat by "resources", nested resources, and non-RESTful routes.
Is there a way to run a command on the rails console to map a URL and figure out exactly which controller, which method, which http method(s), and the exact value of a parameter hash from a given URL?
Is it then possible to then run the controller method with the parameters hash on the rails console and get the output of the controller sent to STDOUT? If so, how?
Try these
Rails.application.routes.named_routes.each{|p,s| puts p,s}
Rails.application.routes.url_helpers.my_path_helper # To get url for a path helper
or
route = Rails.application.routes
route.recognize_path "/poweruser/3" # will give you result exactly you wanted
try it with running
bundle exec rake routes
See Determine if path exists as route in Rails controller
Rails.application.routes.recognize_path