Home > Software engineering >  Error using alias for routes in Ruby on Rails
Error using alias for routes in Ruby on Rails

Time:07-23

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?

CodePudding user response:

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.

  • Related