Tony Marshle
Tony Marshle

Reputation: 127

How does get method call in routes.rb actually create named routes

In a rails routes.rb file, say I have added the code

get "/articles" => "A#B", as: "arts"

Then by simply including the code

as: "arts"

how, and in which file does the "arts_path" method get created?

Upvotes: 1

Views: 1458

Answers (4)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230346

I'll answer the "how". This is called "metaprogramming". Code that writes code. Here is a super-simplified example of how this could be implemented.

get is a method which simply calls define_method (directly or via a few levels of indirection).

module Routable
  def get(resource, as: nil)
    method_name = "#{as || resource.to_s}_path" 
    define_method(method_name) do
      "/#{resource}"
    end
  end
end

class Routes
  extend Routable

  get :products
  get :users, as: :customers
end


routes = Routes.new
routes.respond_to?(:users_path) # => false
routes.respond_to?(:customers_path) # => true
routes.respond_to?(:products_path) # => true
routes.customers_path # => "/users"

As for the "where it is actually happening", you're welcome to peruse rails' code, now that you know what to look for.

Upvotes: 1

Reuben Mallaby
Reuben Mallaby

Reputation: 5763

The routes are created in the Rails library code in the ActionDispatch module.

You can read the code in the GutHub repository in the methods add and add route.

Upvotes: 0

current_user
current_user

Reputation: 1202

Prefix      Verb    URI Pattern                  Controller#Action  Named Helper

articles     GET    /articles(.:format)           articles#index   articles_path or articles_url

The prefix like articles is a path helpers and so the named helper is articles_path or articles_url .

The bottom line is when you call helpers like link_to or form_tag etc - they will require paths to populate different actions in your app's routing structure.

As Rails favours convention over configuration & DRY programming, meaning if you can reference these path helpers over using standard urls, it will allow you to make one reference & chance the route as required

eg:

Calling articles_path is far more powerful than referencing /articles every time

The prefix let's you use shortcuts such as articles_path or articles_url in your controllers and views. These come in very handy when doing things like redirecting users to a specific page or generating dynamic links.

To customize the path helper, you can change the reference in the routes file, like this:

 GET '/articles', to: 'articles#index', as: 'all_articles'

This would change the prefix to all_articles_path instead of articles_path

This allows you to define custom routes / path helpers, allowing you to call those as you wish

Upvotes: 0

Stuart
Stuart

Reputation: 1134

Usually, in rails, your routes will be mapped to a controller method.

What you are doing is setting the route as a GET, accessible through the endpoint /articles.

You are setting this to map to a method defined in controller A, the method signature being B. The as keyword is simply naming the route.

E.g., in your config/routes.rb you may have

get "/articles" => "arts#articles", as: "arts"

Which would direct the /articles GET request to, a method articles defined in the controller:

/app/controllers/arts_controller.rb

I'm not sure what you mean by:

Then by simply including the code

as: "arts"

I hope this helps.

Upvotes: 0

Related Questions