简体   繁体   中英

Error using alias for routes in Ruby on Rails

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM