Rails 5 删除目录名称后编辑和销毁的嵌套路由错误
Rails 5 nested routing error for edit and destroy after removing directory names
我为我的应用程序设置了模型位置、类别、子类别和产品的嵌套路由。我通过附加 , path: ' ' to each route
删除了目录路径,就像我为我的页面模型和根路径所做的那样。该应用程序运行完美,除非我单击编辑并销毁链接。我收到的错误是 can't find record with friendly id: "edit"
旁注,我知道大多数人不同意嵌套 4 深,但这样做是出于一个非常具体的原因,所以我得到了固定的 url 模式,搜索引擎将对其进行索引和排名。如何删除目录并保留编辑?
Rails.application.routes.draw do
mount StripeEvent::Engine, at: '/webhooks/stripe'
devise_for :users
resources :users
resources :locations do
resources :categories, path: '' do
resources :subcategories, path: '' do
resources :products, path: ''
end
end
end
root 'pages#show', defaults: { id: 'my-little-carnival' } # shows home page as default from pages controller
get '/pages/list' => 'pages#index' # sets pages index page before setting path to ''
resources :pages, path: ''
end
栈顶跟踪
Started GET "/locations" for 127.0.0.1 at 2017-11-28 20:41:37 -0800
Processing by LocationsController#index as HTML
Rendering locations/index.html.erb within layouts/application
Location Load (0.3ms) SELECT "locations".* FROM "locations"
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
Rendered locations/index.html.erb within layouts/application (4.2ms)
Rendered layouts/_main-menu.html.erb (0.2ms)
Rendered layouts/_header.html.erb (3.5ms)
Rendered layouts/_standard-page.html.erb (0.3ms)
Rendered layouts/_footer.html.erb (0.6ms)
Completed 200 OK in 74ms (Views: 71.9ms | ActiveRecord: 0.7ms)
Started GET "/locations/san-diego/edit" for 127.0.0.1 at 2017-11-28 20:41:39 -0800
Processing by SubcategoriesController#index as HTML
Parameters: {"location_id"=>"san-diego", "category_id"=>"edit"}
Location Load (0.3ms) SELECT "locations".* FROM "locations" WHERE "locations"."slug" = LIMIT [["slug", "san-diego"], ["LIMIT", 1]]
Category Load (0.4ms) SELECT "categories".* FROM "categories" WHERE "categories"."slug" = LIMIT [["slug", "edit"], ["LIMIT", 1]]
Completed 404 Not Found in 3ms (ActiveRecord: 0.7ms)
好吧,如果您真的想要保留您的格式,如何:
resources :locations, param: :location_id
base = "locations/:location_id"
['category','subcategory','product'].each do |seg|
get "#{base}/#{seg.pluralize}", to: "#{seg.pluralize}#index", as: seg.pluralize
post "#{base}/#{seg.pluralize}", to: "#{seg.pluralize}#create"
get "#{base}/#{seg.pluralize}/new", to: "#{seg.pluralize}#new", as: "new_#{seg}"
get "#{base}/:#{seg}_id/edit", to: "#{seg.pluralize}#edit", as: "edit_#{seg}"
get "#{base}/:#{seg}_id", to: "#{seg.pluralize}#show", as: seg
patch "#{base}/:#{seg}_id", to: "#{seg.pluralize}#update"
put "#{base}/:#{seg}_id", to: "#{seg.pluralize}#update"
delete "#{base}/:#{seg}_id", to: "#{seg.pluralize}#destroy"
base << "/:#{seg}_id"
end
产生:
Prefix Verb URI Pattern Controller#Action
locations GET /locations(.:format) locations#index
POST /locations(.:format) locations#create
new_location GET /locations/new(.:format) locations#new
edit_location GET /locations/:location_id/edit(.:format) locations#edit
location GET /locations/:location_id(.:format) locations#show
PATCH /locations/:location_id(.:format) locations#update
PUT /locations/:location_id(.:format) locations#update
DELETE /locations/:location_id(.:format) locations#destroy
categories GET /locations/:location_id/categories(.:format) categories#index
POST /locations/:location_id/categories(.:format) categories#create
new_category GET /locations/:location_id/categories/new(.:format) categories#new
edit_category GET /locations/:location_id/:category_id/edit(.:format) categories#edit
category GET /locations/:location_id/:category_id(.:format) categories#show
PATCH /locations/:location_id/:category_id(.:format) categories#update
PUT /locations/:location_id/:category_id(.:format) categories#update
DELETE /locations/:location_id/:category_id(.:format) categories#destroy
subcategories GET /locations/:location_id/:category_id/subcategories(.:format) subcategories#index
POST /locations/:location_id/:category_id/subcategories(.:format) subcategories#create
new_subcategory GET /locations/:location_id/:category_id/subcategories/new(.:format) subcategories#new
edit_subcategory GET /locations/:location_id/:category_id/:subcategory_id/edit(.:format) subcategories#edit
subcategory GET /locations/:location_id/:category_id/:subcategory_id(.:format) subcategories#show
PATCH /locations/:location_id/:category_id/:subcategory_id(.:format) subcategories#update
PUT /locations/:location_id/:category_id/:subcategory_id(.:format) subcategories#update
DELETE /locations/:location_id/:category_id/:subcategory_id(.:format) subcategories#destroy
products GET /locations/:location_id/:category_id/:subcategory_id/products(.:format) products#index
POST /locations/:location_id/:category_id/:subcategory_id/products(.:format) products#create
new_product GET /locations/:location_id/:category_id/:subcategory_id/products/new(.:format) products#new
edit_product GET /locations/:location_id/:category_id/:subcategory_id/:product_id/edit(.:format) products#edit
product GET /locations/:location_id/:category_id/:subcategory_id/:product_id(.:format) products#show
PATCH /locations/:location_id/:category_id/:subcategory_id/:product_id(.:format) products#update
PUT /locations/:location_id/:category_id/:subcategory_id/:product_id(.:format) products#update
DELETE /locations/:location_id/:category_id/:subcategory_id/:product_id(.:format) products#destroy
所以,这样做让我觉得很脏,但这是通过为编辑和新方法添加另一组路由来实现的。
resources :locations, only: [:edit, :new] do
resources :categories, only: [:edit, :new] do
resources :subcategories, only: [:edit, :new] do
resources :products, only: [:edit, :new]
end
end
end
resources :locations do
resources :categories, path: '' do
resources :subcategories, path: '' do
resources :products, path: ''
end
end
end
我为我的应用程序设置了模型位置、类别、子类别和产品的嵌套路由。我通过附加 , path: ' ' to each route
删除了目录路径,就像我为我的页面模型和根路径所做的那样。该应用程序运行完美,除非我单击编辑并销毁链接。我收到的错误是 can't find record with friendly id: "edit"
旁注,我知道大多数人不同意嵌套 4 深,但这样做是出于一个非常具体的原因,所以我得到了固定的 url 模式,搜索引擎将对其进行索引和排名。如何删除目录并保留编辑?
Rails.application.routes.draw do
mount StripeEvent::Engine, at: '/webhooks/stripe'
devise_for :users
resources :users
resources :locations do
resources :categories, path: '' do
resources :subcategories, path: '' do
resources :products, path: ''
end
end
end
root 'pages#show', defaults: { id: 'my-little-carnival' } # shows home page as default from pages controller
get '/pages/list' => 'pages#index' # sets pages index page before setting path to ''
resources :pages, path: ''
end
栈顶跟踪
Started GET "/locations" for 127.0.0.1 at 2017-11-28 20:41:37 -0800
Processing by LocationsController#index as HTML
Rendering locations/index.html.erb within layouts/application
Location Load (0.3ms) SELECT "locations".* FROM "locations"
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ORDER BY "users"."id" ASC LIMIT [["id", 1], ["LIMIT", 1]]
Rendered locations/index.html.erb within layouts/application (4.2ms)
Rendered layouts/_main-menu.html.erb (0.2ms)
Rendered layouts/_header.html.erb (3.5ms)
Rendered layouts/_standard-page.html.erb (0.3ms)
Rendered layouts/_footer.html.erb (0.6ms)
Completed 200 OK in 74ms (Views: 71.9ms | ActiveRecord: 0.7ms)
Started GET "/locations/san-diego/edit" for 127.0.0.1 at 2017-11-28 20:41:39 -0800
Processing by SubcategoriesController#index as HTML
Parameters: {"location_id"=>"san-diego", "category_id"=>"edit"}
Location Load (0.3ms) SELECT "locations".* FROM "locations" WHERE "locations"."slug" = LIMIT [["slug", "san-diego"], ["LIMIT", 1]]
Category Load (0.4ms) SELECT "categories".* FROM "categories" WHERE "categories"."slug" = LIMIT [["slug", "edit"], ["LIMIT", 1]]
Completed 404 Not Found in 3ms (ActiveRecord: 0.7ms)
好吧,如果您真的想要保留您的格式,如何:
resources :locations, param: :location_id
base = "locations/:location_id"
['category','subcategory','product'].each do |seg|
get "#{base}/#{seg.pluralize}", to: "#{seg.pluralize}#index", as: seg.pluralize
post "#{base}/#{seg.pluralize}", to: "#{seg.pluralize}#create"
get "#{base}/#{seg.pluralize}/new", to: "#{seg.pluralize}#new", as: "new_#{seg}"
get "#{base}/:#{seg}_id/edit", to: "#{seg.pluralize}#edit", as: "edit_#{seg}"
get "#{base}/:#{seg}_id", to: "#{seg.pluralize}#show", as: seg
patch "#{base}/:#{seg}_id", to: "#{seg.pluralize}#update"
put "#{base}/:#{seg}_id", to: "#{seg.pluralize}#update"
delete "#{base}/:#{seg}_id", to: "#{seg.pluralize}#destroy"
base << "/:#{seg}_id"
end
产生:
Prefix Verb URI Pattern Controller#Action
locations GET /locations(.:format) locations#index
POST /locations(.:format) locations#create
new_location GET /locations/new(.:format) locations#new
edit_location GET /locations/:location_id/edit(.:format) locations#edit
location GET /locations/:location_id(.:format) locations#show
PATCH /locations/:location_id(.:format) locations#update
PUT /locations/:location_id(.:format) locations#update
DELETE /locations/:location_id(.:format) locations#destroy
categories GET /locations/:location_id/categories(.:format) categories#index
POST /locations/:location_id/categories(.:format) categories#create
new_category GET /locations/:location_id/categories/new(.:format) categories#new
edit_category GET /locations/:location_id/:category_id/edit(.:format) categories#edit
category GET /locations/:location_id/:category_id(.:format) categories#show
PATCH /locations/:location_id/:category_id(.:format) categories#update
PUT /locations/:location_id/:category_id(.:format) categories#update
DELETE /locations/:location_id/:category_id(.:format) categories#destroy
subcategories GET /locations/:location_id/:category_id/subcategories(.:format) subcategories#index
POST /locations/:location_id/:category_id/subcategories(.:format) subcategories#create
new_subcategory GET /locations/:location_id/:category_id/subcategories/new(.:format) subcategories#new
edit_subcategory GET /locations/:location_id/:category_id/:subcategory_id/edit(.:format) subcategories#edit
subcategory GET /locations/:location_id/:category_id/:subcategory_id(.:format) subcategories#show
PATCH /locations/:location_id/:category_id/:subcategory_id(.:format) subcategories#update
PUT /locations/:location_id/:category_id/:subcategory_id(.:format) subcategories#update
DELETE /locations/:location_id/:category_id/:subcategory_id(.:format) subcategories#destroy
products GET /locations/:location_id/:category_id/:subcategory_id/products(.:format) products#index
POST /locations/:location_id/:category_id/:subcategory_id/products(.:format) products#create
new_product GET /locations/:location_id/:category_id/:subcategory_id/products/new(.:format) products#new
edit_product GET /locations/:location_id/:category_id/:subcategory_id/:product_id/edit(.:format) products#edit
product GET /locations/:location_id/:category_id/:subcategory_id/:product_id(.:format) products#show
PATCH /locations/:location_id/:category_id/:subcategory_id/:product_id(.:format) products#update
PUT /locations/:location_id/:category_id/:subcategory_id/:product_id(.:format) products#update
DELETE /locations/:location_id/:category_id/:subcategory_id/:product_id(.:format) products#destroy
所以,这样做让我觉得很脏,但这是通过为编辑和新方法添加另一组路由来实现的。
resources :locations, only: [:edit, :new] do
resources :categories, only: [:edit, :new] do
resources :subcategories, only: [:edit, :new] do
resources :products, only: [:edit, :new]
end
end
end
resources :locations do
resources :categories, path: '' do
resources :subcategories, path: '' do
resources :products, path: ''
end
end
end