Rails - Find out where a url-helper points - ruby-on-rails

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.

Related

Rails Get URL Path In View By Route Name

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.

Accessing engine routes from Rails.application.routes.url_helpers

I would like to know how to access engine routes by using Rails.application.routes.url_helpers.
I have a factory object that creates a string including a dynamically generated url. Currently, I can generate urls by using Rails.application.routes.url_helpers.(INSERT PATH NAME).
However, it can only access the routes in the main app. I cannot access the routes of engines mounted on the main app.
Things I have tried
I've tried to use Rails.application.routes.engine_name where engine_name is the name under which the engine is mounted on the main app.
I've tried to use MyEngine::Engine.routes.url_helpers to access the routes in the Engine. It works, but I would like to use Rails.application.routes.url_helpers because there are many factory objects like this one, and they are all inheriting from the superclass that delgates url_helper to Rails.application.routes.
Any suggestions? Let me know if any clarification is needed.
You have to use engine route proxy method.
From your example call the url helper using the following syntax as example:
my_engine_engine.articles_path
To rename the proxy method helper, just edit the routes config file inside your rails application:
mount MyEngine::Engine => '/app', :as => 'my_engine'
so you can now call the previous example:
my_engine.articles_path
Assuming that your engine namespace is EngineName, it should be:
EngineName::Engine.routes.url_helpers.some_engine_path

In Rails, is <resource>_path automatically available?

This seems like a basic question, but does Rails automatically interpret <resource>_path as a route after you have created the route (by understanding the additional _path)?
Creating a resourceful route will also expose a number of helpers to the controllers and views in your application. In the case of resources :photos:
photos_path returns /photos
new_photo_path returns /photos/new
Each of these helpers has a corresponding _url helper (such as photos_url) which returns the same path prefixed with the current host, port and path prefix.
Yes, but it only works on the controllers and views, not in the models.
Resources generates named routes for use in controllers and views.
Routes can be named for easy reference. And named helpers are available for named routes. For example:
If you define a named route:
root to: 'blogs#index'
Then you can use named helpers _url & _path:
root_url # => 'http://www.example.com/'
root_path # => '/'
_path provides a site root relative path which is used most frequently. _url provides the full url including protocol and server name and is used for generating urls for external use (like you need a shareable 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.

Prepend path prefix to all rails routes

I have a setup where nginx serves a rails application inside a specific subfolder
eg. http://myserver/railsapp/ and everything inside gets proxied to rails, if the first subfolder is different, it servers static files from another folder.
I haven't been able to find how to specify this behaviour in rails in an intelligent way. I mean, what I want is to specify an option like Rails.server_prefix = /railsapp so that all the routes get prepended automagically, both on the incoming requests and on the generated links.
You probably want to use the router's scope method with the :path argument:
Rails.application.routes do
scope(:path => '/railsapp') do
# the rest of your routes go here
end
end
See the docs for more info.

Resources