具有多态关联的嵌套路由

时间:2017-05-10 16:56:21

标签: ruby-on-rails ruby

我正在尝试在rails应用程序中实现act_as_votable gem。我让它在博客模型上工作,但评论模型是我被困的地方。当我尝试运行它时,我得到undefined method like_comment_path',这是因为没有像博客那样生成路由。

这是路线文件:

  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
  end

在博客索引上,我能够像路径一样调用博客。

  <div class="btn-group">
    <%= link_to like_blog_path(blog), method: :put, class: "btn btn-default btn-sm" do %>
      <span class="glyphicon glyphicon-chevron-up"></span>
      Upvote
      <%= blog.get_upvotes.size %>
    <% end %>
    <%= link_to dislike_blog_path(blog), method: :put, class: "btn btn-default btn-sm" do %>
      <span class="glyphicon glyphicon-chevron-down"></span>
      Downvote
      <%= blog.get_downvotes.size %>
    <% end %>
  </div>

但是,我在评论部分遇到了麻烦。

  <div class="btn-group">
    <%= link_to like_comment_path(@commentable, comment), method: :put, class: "btn btn-default btn-sm" do %>
      <span class="glyphicon glyphicon-chevron-up"></span>
      Upvote
      <%= comment.get_upvotes.size %>
    <% end %>
    <%= link_to dislike_comment_path(@commentable, comment), method: :put, class: "btn btn-default btn-sm" do %>
      <span class="glyphicon glyphicon-chevron-down"></span>
      Downvote
      <%= comment.get_downvotes.size %>
    <% end %>
  </div>

当我运行rake路线时,评论没有出现。

    Prefix Verb   URI Pattern                                 Controller#Action
                    root GET    /                                           glips#index
              pages_home GET    /pages/home(.:format)                       pages#home
             pages_about GET    /pages/about(.:format)                      pages#about
           pages_contact GET    /pages/contact(.:format)                    pages#contact
                     tag GET    /tags/:tag(.:format)                        glips#index
        new_user_session GET    /login(.:format)                            devise/sessions#new
            user_session POST   /login(.:format)                            devise/sessions#create
    destroy_user_session DELETE /logout(.:format)                           devise/sessions#destroy
       new_user_password GET    /password/new(.:format)                     devise/passwords#new
      edit_user_password GET    /password/edit(.:format)                    devise/passwords#edit
           user_password PATCH  /password(.:format)                         devise/passwords#update
                         PUT    /password(.:format)                         devise/passwords#update
                         POST   /password(.:format)                         devise/passwords#create
cancel_user_registration GET    /cancel(.:format)                           devise/registrations#cancel
   new_user_registration GET    /register(.:format)                         devise/registrations#new
  edit_user_registration GET    /edit(.:format)                             devise/registrations#edit
       user_registration PATCH  /                                           devise/registrations#update
                         PUT    /                                           devise/registrations#update
                         DELETE /                                           devise/registrations#destroy
                         POST   /                                           devise/registrations#create
               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
                   blogs GET    /blogs(.:format)                            blogs#index
                         POST   /blogs(.:format)                            blogs#create
                new_blog GET    /blogs/new(.:format)                        blogs#new
               edit_blog GET    /blogs/:id/edit(.:format)                   blogs#edit
                    blog GET    /blogs/:id(.:format)                        blogs#show
                         PATCH  /blogs/:id(.:format)                        blogs#update
                         PUT    /blogs/:id(.:format)                        blogs#update
                         DELETE /blogs/:id(.:format)                        blogs#destroy
                   users GET    /users(.:format)                            users#index
                         GET    /users/:id(.:format)                        users#show
                    user GET    /users/:id(.:format)                        users#show

我无法弄清楚如何使评论路径与路线一起工作。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您是否正在寻找在:blogs 中嵌套:comments 资源?否则您的路线应如下所示:

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

(注意你在:comments块的开头缺少了 do ,这导致了一个奇怪的块结束/嵌套)