【问题标题】:Rails nested routes not working as expectedRails 嵌套路由未按预期工作
【发布时间】:2019-08-15 10:17:20
【问题描述】:

我的路线中有这样的嵌套资源。这些在我的其他 rails 5 应用程序上完美运行,但在我的 rails 6 应用程序上却不能。我不明白为什么它只识别第一层嵌套的东西。

  resources :blogs do
    member do
      put 'like', to: 'blogs#upvote'
      put 'dislike', to: 'blogs#downvote'
    end
    resources :comments
      member do
        put 'like', to: 'comments#upvote'
        put 'dislike', to: 'comments#downvote'
      end
      resources :notations
  end

这是 rake 路线给我的:

 blogs_user GET      /users/:id/blogs(.:format)                                                               users#blogs
                             like_blog PUT      /blogs/:id/like(.:format)                                                                blogs#upvote
                          dislike_blog PUT      /blogs/:id/dislike(.:format)                                                             blogs#downvote
                         blog_comments GET      /blogs/:blog_id/comments(.:format)                                                       comments#index
                                       POST     /blogs/:blog_id/comments(.:format)                                                       comments#create
                      new_blog_comment GET      /blogs/:blog_id/comments/new(.:format)                                                   comments#new
                     edit_blog_comment GET      /blogs/:blog_id/comments/:id/edit(.:format)                                              comments#edit
                          blog_comment GET      /blogs/:blog_id/comments/:id(.:format)                                                   comments#show
                                       PATCH    /blogs/:blog_id/comments/:id(.:format)                                                   comments#update
                                       PUT      /blogs/:blog_id/comments/:id(.:format)                                                   comments#update
                                       DELETE   /blogs/:blog_id/comments/:id(.:format)                                                   comments#destroy
                                       PUT      /blogs/:id/like(.:format)                                                                comments#upvote
                                       PUT      /blogs/:id/dislike(.:format)                                                             comments#downvote
                             notations GET      /blogs/:id/notations(.:format)                                                           notations#index
                                       POST     /blogs/:id/notations(.:format)                                                           notations#create
                          new_notation GET      /blogs/:id/notations/new(.:format)                                                       notations#new
                         edit_notation GET      /blogs/:id/notations/:id/edit(.:format)                                                  notations#edit
                              notation GET      /blogs/:id/notations/:id(.:format)                                                       notations#show
                                       PATCH    /blogs/:id/notations/:id(.:format)                                                       notations#update
                                       PUT      /blogs/:id/notations/:id(.:format)                                                       notations#update
                                       DELETE   /blogs/:id/notations/:id(.:format)                                                       notations#destroy

例如,在我的其他应用上,它会产生

/blogs/:blog_id/comments/:id/like

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我复制了您的路线并在两个应用程序(Rails 5 和 Rails 6)中进行了复制,并且都生成了相同的路线(没有三个嵌套级别)。如果你想要/blogs/:blog_id/comments/:id/like 路由,你必须做一个小改动。

      resources :blogs do
        member do
          put 'like', to: 'blogs#upvote'
          put 'dislike', to: 'blogs#downvote'
        end
        resources :comments do
          member do
            put 'like', to: 'comments#upvote'
            put 'dislike', to: 'comments#downvote'
          end
        end
        resources :notations
      end
    

    【讨论】:

      【解决方案2】:

      您缺少“do”“end”块语法

      resources :blogs do
        member do
          put 'like', to: 'blogs#upvote'
          put 'dislike', to: 'blogs#downvote'
        end
        resources :comments do # here
          member do
            put 'like', to: 'comments#upvote'
            put 'dislike', to: 'comments#downvote'
          end
          resources :notations
        end # and here
      end
      

      无论如何,rails 指南不鼓励多于两层的嵌套。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-13
        • 2022-01-22
        • 2014-05-02
        • 2016-08-19
        • 2015-09-09
        • 1970-01-01
        • 2012-10-04
        相关资源
        最近更新 更多