In Rails, is <resource>_path automatically available? - ruby-on-rails

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).

Related

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

Insert routes programmatically in Rails

I'm creating an engine that needs to insert some routes into the application's router. For this particular gem, I'd rather not application's routes.rb if possible. Is there a way to insert routes at a particular location in the router via code? I'm looking for an API that does something like:
Rails.application.routes.insert("resources :foos", :before => "some string in routes.rb")
If I create a config/routes.rb inside the engine and define some routes, this kind of works. Rails is smart enough to mix the engine's routes into the application's routes, but it tacks them on at the end of the route list. I need them to appear at the beginning so the engine's routes take priority.
I'm aware that I can namespace the routes by mounting the engine in the application's routes.rb, but this creates a routing structure that I don't really want. I want the engine's routes to look they are a part of the application by defining some routes in the actual application.
I have a workaround which is to add the following to the application's routes.rb.
Rails.application.routes.draw do
MyEngine.setup_routes(self)
#...other routes below
end
MyEngine.setup_routes looks like
def self.setup_routes(map)
map.get 'a_path', :to => 'a_controller#a_path'
end
This at least allows me to control the point where the routes get defined in the application's route list, but the user still has to manually update his routes.rb (or I have to build an installer that does it). It seems like there should be a way to tell rails to tack some routes onto the start of the route list...

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.

How do I set root path to '/' without controller and action in Rails?

In my routes.rb I want to set the root_path to '/' without specifying any controller or action. Is that possible?
I tried root to: '/' but it's complaining, that no controller or action is specified.
Updated Info:
I'm using the comfortable-mexican-sofa CMS, and my root_path should go to the cms root path.
At the moment I'm just using redirect_to '/' in my controllers and this is working, but I thought it would be nicer to use root_path.
Try
root :to => "cms/content#show"
From the docs: https://github.com/comfy/comfortable-mexican-sofa/wiki/Pages
The first page you create is the homepage, and therefore you cannot specify a slug. By default, you should be able to access the homepage without specifying your root route. However, if you or a gem you are using need access to the root_path helper, specify root :to => "cms/content#show" in your routes.
Further to the accepted answer, the routes of Rails is all about creating the paths for your application
According to the ActionDispatch::Routing middleware documentation:
The routing module provides URL rewriting in native Ruby. It's a way to
redirect incoming requests to controllers and actions. This replaces
mod_rewrite rules. Best of all, Rails' Routing works with any web
server. Routes are defined in config/routes.rb
This means the job of routes.rb is to take the inbound request (/, /posts, etc) & redirect to ruby-centric code. Ruby doesn't know what / is -- it has to have a controller / action combo
That's why you'd need something like:
#config/routes.rb
root to: "controller#action"

rubymine generate routes.rb

I am new to Ruby on Rails and have some problems.
For the development I use RubyMine IDE, I manage to create models, controllers and views, but I have problems with the routing. By default, routes.rb file contains only this method Apis::Application.routes.draw do with an empty body.
For example, I create a controller TestController, then the index method and in routes.rb I add this instruction resources :test. So far, it works fine. But if I add another method, let's say method1 (and the view) I can't reach it in a browser http://localhost:3000/test/method1.
What else should I add in routes.rb file?
Is there any way to make the routing automatically from the IDE, with less editing the routes file?
resources :test
is a resourceful route which provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database
you can uncomment in your routes to enable the controller action mapping.
match ':controller(/:action(/:id(.:format)))'
or use -
match "/test/method1" => "test#method1"
Detailed routes info # http://guides.rubyonrails.org/routing.html

Resources