1

I have a route with a namespace

namespace :publishers do
    resources :authors
    get 'books' :to => 'books'
    get 'books/custom_report/:id', :to => "curriculos#custom_report"
end

Often I will have to make links in my application and I know than it`s possible to use a alias for routing like this:

<%= link_to "Books", publishers_books_path %>

I call that publishers_books_path a alias route, does this is the correct name?

Furthermore, I still not able to understand the logic with this alias naming because i can`t use for a new or a custom action like this

link_to 'Show the report', publishers_books_custom_report_path(params[:id]) 

I'm always get a error of undefined_method for publishers_books_custom_report_path

So there`s some questions

  1. First of all whats it`s the correct name of this feature in RoR?
  2. How I can use the custom_report as aliases to link_to? And also if i need to use some basic operations like new, update, insert?
  3. Can someone give me the link to the documentation to really understant that feature?
1
  • There's a missing comma in get 'books' :to => 'books'. Is that a typo in the question or the code? Commented Jul 22, 2022 at 19:47

1 Answer 1

3

First of all whats it`s the correct name of this feature in RoR?

The docs use "path helper" and "named route helpers" interchangeably.

How I can use the custom_report as aliases to link_to?

Use rails route or visit /rails/info/routes in your dev server to get a list of all your routes, their helpers, and controller actions.

Apparently it is publishers_path which doesn't seem right. You can fix this with an as.

get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report

And also if i need to use some basic operations like new, update, insert?

A get declares just that one specific route. If you need all the operations on a model, declare it as a resource.

  namespace :publishers do
    resource :authors
    resource :books
    get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report
  end

Can someone give me the link to the documentation to really understand that feature?

Rails Routing From The Outside In.

Sign up to request clarification or add additional context in comments.

2 Comments

Good answer. I would add that get 'books/custom_report/:id' is quite unidimatic. The idiomatic way would be get 'books/:id/custom_report' see guides.rubyonrails.org/routing.html#adding-more-restful-actions and also the comment on avoiding deep nesting.
resource :books do; get :custom_report, on: :member, controller: 'curriculos': end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.