【问题标题】:What is best method to adding a custom route to a shallow nested resource将自定义路由添加到浅层嵌套资源的最佳方法是什么
【发布时间】:2014-01-19 19:21:14
【问题描述】:

我正在使用 act_as_votable gem 来喜欢来自两个不同模型的对象。

在 Rails 控制台中一切正常。

我无法在其资源被嵌套的控制器中创建我需要的路由。

路由文件目前看起来像这样。

KitsIo::Application.routes.draw do

  resources :kits do
  get :like, :on => :member
  resources :things, shallow: true
    get :like, :on => :member
 end


 root 'kits#index'

end

运行 rake 路线:

    like_kit_path    GET     /kits/:id/like(.:format)    kits#like
    kit_things_path  GET     /kits/:kit_id/things(.:format)  things#index
    POST     /kits/:kit_id/things(.:format)  things#create
    new_kit_thing_path   GET     /kits/:kit_id/things/new(.:format)  things#new
    edit_thing_path  GET     /things/:id/edit(.:format)  things#edit
    thing_path   GET     /things/:id(.:format)   things#show
    PATCH    /things/:id(.:format)   things#update
    PUT  /things/:id(.:format)   things#update
    DELETE   /things/:id(.:format)   things#destroy
    GET  /kits/:id/like(.:format)    kits#like
    kits_path    GET     /kits(.:format)     kits#index
    POST     /kits(.:format)     kits#create
    new_kit_path     GET     /kits/new(.:format)     kits#new
    edit_kit_path    GET     /kits/:id/edit(.:format)    kits#edit
    kit_path     GET     /kits/:id(.:format)     kits#show
    PATCH    /kits/:id(.:format)     kits#update
    PUT  /kits/:id(.:format)     kits#update
    DELETE   /kits/:id(.:format)     kits#destroy

    root_path    GET     /   kits#index

到 like_kit_path 的路由已正确创建,我可以从 Kits 控制器中喜欢一个工具包。

我要解决的问题是能够创建一个 like_things_path 来路由到 things 控制器。

like_things_path GET /things/:id/like(.:format) things#like

如果我把路由文件改成这样:

KitsIo::Application.routes.draw do


resources :kits do
    get :like, :on => :member
    resources :things do
    get :like, :on => :member
   end
end




  root 'kits#index'

 end

然后正确地创建到 things 控制器的路由。

like_kit_path    GET     /kits/:id/like(.:format)    kits#like
like_kit_thing_path  GET     /kits/:kit_id/things/:id/like(.:format)     things#like
kit_things_path  GET     /kits/:kit_id/things(.:format)  things#index
POST     /kits/:kit_id/things(.:format)  things#create
new_kit_thing_path   GET     /kits/:kit_id/things/new(.:format)  things#new
edit_kit_thing_path  GET     /kits/:kit_id/things/:id/edit(.:format)     things#edit
kit_thing_path   GET     /kits/:kit_id/things/:id(.:format)  things#show
PATCH    /kits/:kit_id/things/:id(.:format)  things#update
PUT  /kits/:kit_id/things/:id(.:format)  things#update
DELETE   /kits/:kit_id/things/:id(.:format)  things#destroy
kits_path    GET     /kits(.:format)     kits#index
POST     /kits(.:format)     kits#create
new_kit_path     GET     /kits/new(.:format)     kits#new
edit_kit_path    GET     /kits/:id/edit(.:format)    kits#edit
kit_path     GET     /kits/:id(.:format)     kits#show
PATCH    /kits/:id(.:format)     kits#update
PUT  /kits/:id(.:format)     kits#update
DELETE   /kits/:id(.:format)     kits#destroy
root_path    GET     /   kits#index

除了路由文件的第二个示例之外,还有其他方法可以创建到 things 控制器的自定义路由吗?

虽然这会创建正确的路线,但它会破坏当前正在运行的大部分代码。

在此先感谢您在帮助我解决此问题时提供的任何帮助,我们将不胜感激。

【问题讨论】:

    标签: controller routing member custom-routes


    【解决方案1】:

    好吧,您在声明路线时犯的一个非常微妙的错误是您没有为“事物”资源提供块。因此,正如您在输出中看到的那样,Rails 正在为您的路由中的同一操作创建两个重复的路径,即 kits_like_path 映射到 kits#like 因为这两个 get :like, on: :member 都只应用于“工具包”资源。 所以只是为了让它在声明“事物”资源时提供块,如下所示:

      resources :kits do
        get :like, :on => :member
        resources :things, shallow: true do
          get :like, :on => :member
        end
      end
    

    rake routes 命令的输出如下:

            like_kit GET    /kits/:id/like(.:format)                 kits#like
          like_thing GET    /things/:id/like(.:format)               things#like
          kit_things GET    /kits/:kit_id/things(.:format)           things#index
                     POST   /kits/:kit_id/things(.:format)           things#create
       new_kit_thing GET    /kits/:kit_id/things/new(.:format)       things#new
          edit_thing GET    /things/:id/edit(.:format)               things#edit
               thing GET    /things/:id(.:format)                    things#show
                     PUT    /things/:id(.:format)                    things#update
                     DELETE /things/:id(.:format)                    things#destroy
                kits GET    /kits(.:format)                          kits#index
                     POST   /kits(.:format)                          kits#create
             new_kit GET    /kits/new(.:format)                      kits#new
            edit_kit GET    /kits/:id/edit(.:format)                 kits#edit
                 kit GET    /kits/:id(.:format)                      kits#show
                     PUT    /kits/:id(.:format)                      kits#update
                     DELETE /kits/:id(.:format)                      kits#destroy
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      相关资源
      最近更新 更多