Ruby on rails Rails 4中自引用关联的路由

Ruby on rails Rails 4中自引用关联的路由,ruby-on-rails,routes,self-reference,nested-routes,Ruby On Rails,Routes,Self Reference,Nested Routes,我正在尝试为自引用嵌套关联设置路由。目标是获取/category\u name/subcategory\u name的URL 我使用友好的URL来获取类别名称,而不是ID。我已经能够将其中的一半用于此: 匹配'/:id'=>'列表#索引',:via=>'get',如:'category' 然而,我试图使子类别正常工作的一切都失败了?我遇到的唯一答案是为子类别创建另一个模型/控制器。如果可能的话,我希望避免这种情况,因为这会增加额外的复杂性,但收获甚微。我真的不需要任何单独方法可能提供的额外方法/

我正在尝试为自引用嵌套关联设置路由。目标是获取
/category\u name/subcategory\u name
的URL

我使用友好的URL来获取类别名称,而不是ID。我已经能够将其中的一半用于此:

匹配'/:id'=>'列表#索引',:via=>'get',如:'category'

然而,我试图使子类别正常工作的一切都失败了?我遇到的唯一答案是为子类别创建另一个模型/控制器。如果可能的话,我希望避免这种情况,因为这会增加额外的复杂性,但收获甚微。我真的不需要任何单独方法可能提供的额外方法/灵活性


这是我的第一个Rails应用程序,请原谅这个问题的基本性质。

没有看到您的routes文件或确切知道控制器的名称(
listing
可能应该是
listings
,因为Rails控制器通常是复数,除非您有意识地将控制器命名为
listing

假设控制器是listings,并且您希望将类别名称和子类别名称传递给该控制器索引方法,我将使我的
匹配| get
路由如下:

get '/:category_name/:subcategory_name', to: 'listings#index', as: 'category'
然后,在ListingsController的索引方法中,我可能(假设提供了它们)检查
params[:category\u name]
params[:subcategory\u name]
,并相应地对它们采取行动


也很有帮助。

没有看到路由文件,也不知道控制器的名称(
listing
可能应该是
listings
,因为Rails控制器通常是复数的,当然除非您有意识地将控制器命名为
listing

假设控制器是listings,并且您希望将类别名称和子类别名称传递给该控制器索引方法,我将使我的
匹配| get
路由如下:

get '/:category_name/:subcategory_name', to: 'listings#index', as: 'category'
然后,在ListingsController的索引方法中,我可能(假设提供了它们)检查
params[:category\u name]
params[:subcategory\u name]
,并相应地对它们采取行动


也很有帮助。

没有看到路由文件,也不知道控制器的名称(
listing
可能应该是
listings
,因为Rails控制器通常是复数的,当然除非您有意识地将控制器命名为
listing

假设控制器是listings,并且您希望将类别名称和子类别名称传递给该控制器索引方法,我将使我的
匹配| get
路由如下:

get '/:category_name/:subcategory_name', to: 'listings#index', as: 'category'
然后,在ListingsController的索引方法中,我可能(假设提供了它们)检查
params[:category\u name]
params[:subcategory\u name]
,并相应地对它们采取行动


也很有帮助。

没有看到路由文件,也不知道控制器的名称(
listing
可能应该是
listings
,因为Rails控制器通常是复数的,当然除非您有意识地将控制器命名为
listing

假设控制器是listings,并且您希望将类别名称和子类别名称传递给该控制器索引方法,我将使我的
匹配| get
路由如下:

get '/:category_name/:subcategory_name', to: 'listings#index', as: 'category'
然后,在ListingsController的索引方法中,我可能(假设提供了它们)检查
params[:category\u name]
params[:subcategory\u name]
,并相应地对它们采取行动


这也很有帮助。

如果我是你,我会这样做:

定义路线:

resources :categories do
  resources :categories, path: '/'
end
哪个
rake路由
显示:

   category_categories GET    /categories/:category_id(.:format)          categories#index
                       POST   /categories/:category_id(.:format)          categories#create
 new_category_category GET    /categories/:category_id/new(.:format)      categories#new
edit_category_category GET    /categories/:category_id/:id/edit(.:format) categories#edit
     category_category GET    /categories/:category_id/:id(.:format)      categories#show
                       PATCH  /categories/:category_id/:id(.:format)      categories#update
                       PUT    /categories/:category_id/:id(.:format)      categories#update
                       DELETE /categories/:category_id/:id(.:format)      categories#destroy
            categories GET    /categories(.:format)                       categories#index
                       POST   /categories(.:format)                       categories#create
          new_category GET    /categories/new(.:format)                   categories#new
         edit_category GET    /categories/:id/edit(.:format)              categories#edit
              category GET    /categories/:id(.:format)                   categories#show
                       PATCH  /categories/:id(.:format)                   categories#update
                       PUT    /categories/:id(.:format)                   categories#update
                       DELETE /categories/:id(.:format)                   categories#destroy
categories\u控制器上
可以识别嵌套的类别(子类别),因为
params[:category\u id]
不是
nil
。如果是
nil
,则操作针对父类别

编辑 添加了路径选项

我想你想要这个:

   category_categories GET    /:category_id(.:format)          categories#index
                       POST   /:category_id(.:format)          categories#create
 new_category_category GET    /:category_id/new(.:format)      categories#new
edit_category_category GET    /:category_id/:id/edit(.:format) categories#edit
     category_category GET    /:category_id/:id(.:format)      categories#show
                       PATCH  /:category_id/:id(.:format)      categories#update
                       PUT    /:category_id/:id(.:format)      categories#update
                       DELETE /:category_id/:id(.:format)      categories#destroy
            categories GET    /                                categories#index
                       POST   /                                categories#create
          new_category GET    /new(.:format)                   categories#new
         edit_category GET    /:id/edit(.:format)              categories#edit
              category GET    /:id(.:format)                   categories#show
                       PATCH  /:id(.:format)                   categories#update
                       PUT    /:id(.:format)                   categories#update
                       DELETE /:id(.:format)                   categories#destroy
您必须在外部路线上执行相同的操作:

resources :categories, path: '/' do
  resources :categories, path: '/'
end

如果我是你,我会这样做:

定义路线:

resources :categories do
  resources :categories, path: '/'
end
哪个
rake路由
显示:

   category_categories GET    /categories/:category_id(.:format)          categories#index
                       POST   /categories/:category_id(.:format)          categories#create
 new_category_category GET    /categories/:category_id/new(.:format)      categories#new
edit_category_category GET    /categories/:category_id/:id/edit(.:format) categories#edit
     category_category GET    /categories/:category_id/:id(.:format)      categories#show
                       PATCH  /categories/:category_id/:id(.:format)      categories#update
                       PUT    /categories/:category_id/:id(.:format)      categories#update
                       DELETE /categories/:category_id/:id(.:format)      categories#destroy
            categories GET    /categories(.:format)                       categories#index
                       POST   /categories(.:format)                       categories#create
          new_category GET    /categories/new(.:format)                   categories#new
         edit_category GET    /categories/:id/edit(.:format)              categories#edit
              category GET    /categories/:id(.:format)                   categories#show
                       PATCH  /categories/:id(.:format)                   categories#update
                       PUT    /categories/:id(.:format)                   categories#update
                       DELETE /categories/:id(.:format)                   categories#destroy
categories\u控制器上
可以识别嵌套的类别(子类别),因为
params[:category\u id]
不是
nil
。如果是
nil
,则操作针对父类别

编辑 添加了路径选项

我想你想要这个:

   category_categories GET    /:category_id(.:format)          categories#index
                       POST   /:category_id(.:format)          categories#create
 new_category_category GET    /:category_id/new(.:format)      categories#new
edit_category_category GET    /:category_id/:id/edit(.:format) categories#edit
     category_category GET    /:category_id/:id(.:format)      categories#show
                       PATCH  /:category_id/:id(.:format)      categories#update
                       PUT    /:category_id/:id(.:format)      categories#update
                       DELETE /:category_id/:id(.:format)      categories#destroy
            categories GET    /                                categories#index
                       POST   /                                categories#create
          new_category GET    /new(.:format)                   categories#new
         edit_category GET    /:id/edit(.:format)              categories#edit
              category GET    /:id(.:format)                   categories#show
                       PATCH  /:id(.:format)                   categories#update
                       PUT    /:id(.:format)                   categories#update
                       DELETE /:id(.:format)                   categories#destroy
您必须在外部路线上执行相同的操作:

resources :categories, path: '/' do
  resources :categories, path: '/'
end

如果我是你,我会这样做:

定义路线:

resources :categories do
  resources :categories, path: '/'
end
哪个
rake路由
显示:

   category_categories GET    /categories/:category_id(.:format)          categories#index
                       POST   /categories/:category_id(.:format)          categories#create
 new_category_category GET    /categories/:category_id/new(.:format)      categories#new
edit_category_category GET    /categories/:category_id/:id/edit(.:format) categories#edit
     category_category GET    /categories/:category_id/:id(.:format)      categories#show
                       PATCH  /categories/:category_id/:id(.:format)      categories#update
                       PUT    /categories/:category_id/:id(.:format)      categories#update
                       DELETE /categories/:category_id/:id(.:format)      categories#destroy
            categories GET    /categories(.:format)                       categories#index
                       POST   /categories(.:format)                       categories#create
          new_category GET    /categories/new(.:format)                   categories#new
         edit_category GET    /categories/:id/edit(.:format)              categories#edit
              category GET    /categories/:id(.:format)                   categories#show
                       PATCH  /categories/:id(.:format)                   categories#update
                       PUT    /categories/:id(.:format)                   categories#update
                       DELETE /categories/:id(.:format)                   categories#destroy
categories\u控制器上
可以识别嵌套的类别(子类别),因为
params[:category\u id]
不是
nil
。如果是
nil
,则操作针对父类别

编辑 添加了路径选项

我想你想要这个:

   category_categories GET    /:category_id(.:format)          categories#index
                       POST   /:category_id(.:format)          categories#create
 new_category_category GET    /:category_id/new(.:format)      categories#new
edit_category_category GET    /:category_id/:id/edit(.:format) categories#edit
     category_category GET    /:category_id/:id(.:format)      categories#show
                       PATCH  /:category_id/:id(.:format)      categories#update
                       PUT    /:category_id/:id(.:format)      categories#update
                       DELETE /:category_id/:id(.:format)      categories#destroy
            categories GET    /                                categories#index
                       POST   /                                categories#create
          new_category GET    /new(.:format)                   categories#new
         edit_category GET    /:id/edit(.:format)              categories#edit
              category GET    /:id(.:format)                   categories#show
                       PATCH  /:id(.:format)                   categories#update
                       PUT    /:id(.:format)                   categories#update
                       DELETE /:id(.:format)                   categories#destroy
您必须在外部路线上执行相同的操作:

resources :categories, path: '/' do
  resources :categories, path: '/'
end

如果我是你,我会这样做:

定义路线:

resources :categories do
  resources :categories, path: '/'
end
哪个
rake路由
显示:

   category_categories GET    /categories/:category_id(.:format)          categories#index
                       POST   /categories/:category_id(.:format)          categories#create
 new_category_category GET    /categories/:category_id/new(.:format)      categories#new
edit_category_category GET    /categories/:category_id/:id/edit(.:format) categories#edit
     category_category GET    /categories/:category_id/:id(.:format)      categories#show
                       PATCH  /categories/:category_id/:id(.:format)      categories#update
                       PUT    /categories/:category_id/:id(.:format)      categories#update
                       DELETE /categories/:category_id/:id(.:format)      categories#destroy
            categories GET    /categories(.:format)                       categories#index
                       POST   /categories(.:format)                       categories#create
          new_category GET    /categories/new(.:format)                   categories#new
         edit_category GET    /categories/:id/edit(.:format)              categories#edit
              category GET    /categories/:id(.:format)                   categories#show
                       PATCH  /categories/:id(.:format)                   categories#update
                       PUT    /categories/:id(.:format)                   categories#update
                       DELETE /categories/:id(.:format)                   categories#destroy
categories\u控制器上
可以识别嵌套的类别(子类别),因为
params[:category\u id]
不是
nil
。如果是
nil
,则操作针对父类别

编辑 添加了路径选项

我想你想要这个:

   category_categories GET    /:category_id(.:format)          categories#index
                       POST   /:category_id(.:format)          categories#create
 new_category_category GET    /:category_id/new(.:format)      categories#new
edit_category_category GET    /:category_id/:id/edit(.:format) categories#edit
     category_category GET    /:category_id/:id(.:format)      categories#show
                       PATCH  /:category_id/:id(.:format)      categories#update
                       PUT    /:category_id/:id(.:format)      categories#update
                       DELETE /:category_id/:id(.:format)      categories#destroy
            categories GET    /                                categories#index
                       POST   /                                categories#create
          new_category GET    /new(.:format)                   categories#new
         edit_category GET    /:id/edit(.:format)              categories#edit
              category GET    /:id(.:format)                   categories#show
                       PATCH  /:id(.:format)                   categories#update
                       PUT    /:id(.:format)                   categories#update
                       DELETE /:id(.:format)                   categories#destroy
您必须在外部路线上执行相同的操作:

resources :categories, path: '/' do
  resources :categories, path: '/'
end