我有一个带有名称空间的路由
namespace :publishers do
resources :authors
get 'books' :to => 'books'
get 'books/custom_report/:id', :to => "curriculos#custom_report"
end
通常,我必须在我的应用程序中创建链接,我知道使用别名进行路由是可能的,如下所示:
<%= link_to "Books", publishers_books_path %>
我称publishers_books_path为别名路由,这是正确的名称吗?
此外,我仍然无法理解这种别名命名的逻辑,因为我不能用于像这样的新操作或自定义操作
link_to 'Show the report', publishers_books_custom_report_path(params[:id])
对于publishers_books_custom_report_path
,我总是得到一个undefined_method的错误
所以有一些问题
- 首先,RoR中该功能的正确名称是什么
- 如何将custom_report用作link_to的别名?如果我需要使用一些基本操作,比如new、update、insert
- 有人能给我文档的链接,让我真正了解这个功能吗
首先,RoR中该功能的正确名称是什么?
文档使用"路径助手";以及";命名的路由助手";可互换。
如何使用custom_report作为link_to的别名?
使用rails route
或访问开发服务器中的/rails/info/routes
来获取所有路由、其助手和控制器操作的列表。
显然是publishers_path
似乎不对。您可以使用as
修复此问题。
get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report
如果我需要使用一些基本操作,如new、update、,插入
get
只声明了一个特定的路由。如果需要对模型执行所有操作,请将其声明为resource
。
namespace :publishers do
resource :authors
resource :books
get 'books/custom_report/:id', to: "curriculos#custom_report", as: :books_custom_report
end
有人能给我文档链接以真正理解该功能吗?
Rails Routing From The Outside In.