ddonche
ddonche

Reputation: 1065

Nested routes with polymorphic association

I'm trying to implement the act_as_votable gem in my rails application. I got it to work on the blogs model, but the comments model is where I'm stuck. I get undefined methodlike_comment_path'` when I try to run it, and it's because there are no routes being generated like there are for the blog.

Here is the routes file:

  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

On the blog index, I am able to call the blog like path just fine.

  <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>

I run into trouble, though, with the comment part.

  <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>

When I run rake routes, nothing for the comments comes up.

    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

I cannot figure out how to make the comment path work with the routes. What am I doing wrong?

Upvotes: 0

Views: 56

Answers (1)

ximes
ximes

Reputation: 131

Are you looking for nesting the :comments resource inside :blogs? Otherwise your routes should look like:

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

(note you had a missing do at beginning of the :comments block which resulted in a weird block ending/nesting)

Upvotes: 1

Related Questions