Ruby on Rails tutorial: difference between route_path and route_url - ruby-on-rails

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

Related

In Ruby On Rails CLI, how can I list Helper Paths?

If I enter bin/rails routes into terminal, I see a list of routes for my Ruby application.
If I visit the address http://localhost:3000/rails/info/routes I see the same routes, but with the additional column of Helper. These helper paths are useful to have a list of to hand, but I don't want to open up the webpage each time.
Is there a way I can run bin/rails routes in terminal, with the extra column to show helper paths? (articles_path, new_articles_path etc)
I'm on Rails 5.1.4.
When you enter bin/rails routes into terminal, you see the prefix column.
That's what you are looking for.
So if there is a prefix: welcome_index, there exists welcome_index_path, welcome_index_url helpers.
You can append _path or _url to prefix to get the name of a route helper.
The helpers are listed in the leftmost column of the output of bin/rails routes. Just append _path or _url.

Rails - Find out where a url-helper points

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.

Rails: Map of path helper methods to URL formats?

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.

Ruby on Rails - Map URL to a controller, method, and parameters

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

How do I write a full path in a controller in Rails 3?

I need to write the full path so need to know what the rails_root domain is. How do I do that? For example:
string = "{RAILS_ROOT}/vendors/#{#vendor.id}"
What is the equivalent of "RAILS_ROOT" to give me what the full domain is for my application? So that in development it would subsstitute localhost:3000 and on my heroku site the right full domain?
You should always avoid, if possible, hard-coding your path, because it is less flexible and more prone to result in broken links in the future. Plus, you can use Rails routing, which is an elegant way to generate everything cohesively in Rails without any need to create the composite parts yourself.
If you have your routes set up properly, you should be able to call:
link_to "View vendor", vendor_url(#vendor.id)
Vendor_url(#vendor.id) in Rails gives you your full URL, which you can then contain in your string variable. Here's how to generate the routes needed for the above:
# in routes.rb
resources :vendors
Try:
File.realpath(RAILS_ROOT)
You could access the request object. request.host_with_port would give you the hostname and port. request.protocol will give you the protocol (http:// or https://). request.fullpath will give you the path with query params.

Resources