2017-11-29 18 views
0

モデルの場所、カテゴリ、サブカテゴリ、および製品のために、自分のアプリのルートをネストしました。私は私のページモデルとルートパスのためにしたように、path: ' ' to each routeを追加してディレクトリパスを削除しました。このアプリはリンクを編集して削除する以外は完全に機能します。私が受け取ったエラーはcan't find record with friendly id: "edit"Rails 5ディレクトリ名を削除した後の編集と破棄のネストされたルーティングエラー

です。私は大部分が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" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["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" = $1 LIMIT $2 [["slug", "san-diego"], ["LIMIT", 1]] 
    Category Load (0.4ms) SELECT "categories".* FROM "categories" WHERE "categories"."slug" = $1 LIMIT $2 [["slug", "edit"], ["LIMIT", 1]] 
Completed 404 Not Found in 3ms (ActiveRecord: 0.7ms) 
+0

パスは上から下に処理されます。私はあなたの問題は、どのように物事を整理し、あなたのパスが予期しない方法で一致していることを考えれば推測しています。しかし、より完全なスタックトレース(最初のいくつかの行)がなくても、良い感じを得るのは難しいです。 – jvillian

+0

@jvillian質問にスタックトレースの先頭部分を追加しました。パスが 'path: '''に設定されているように見え、それはスキップしてその下のコントローラーを使用しています。 –

+0

大変申し訳ありませんが、それはトップですか?私は127.0.0.1で '開始GET"/blah/blah/blah "となるだろうと考えていました。私はまだRails 4に入っています。多分今は違うかもしれません。 – jvillian

答えて

1

Welp、あなた本当には約どのように、あなたが持っていたその形式を維持したい場合:

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 
0

だから、私はこれを行うのが気になりませんが、これは編集と新しい方法の別のルートセットを追加することによって機能します。

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 
関連する問題