All threads / problem with like link in nested resource

Ask A Question

Notifications

You’re not receiving notifications from this thread.

problem with like link in nested resource

Pablo Seo asked in Rails

Hi Chris im doing the episode 'liking posts' i have difficult to do with deep nested resource,
my actual nested resources looks:

A. Routes:

  resources :users do
      resources :catalogs do
          resources :pages do
              resources :types do
                  resources :like, module: :types
              end
          end
      end
  end

B. link:

= link_to 'Like', [current_user, @catalog, @page, @type, @like], method: :post

C. Rake Routes:

user_catalog_page_type_like_index GET    /users/:user_id/catalogs/:catalog_id/pages/:page_id/types/:type_id/like(.:format)       types/like#index                                                   ____________________________ POST   /users/:user_id/catalogs/:catalog_id/pages/:page_id/types/:type_id/like(.:format)      types/like#create  new_user_catalog_page_type_like  GET     /users/:user_id/catalogs/:catalog_id/pages/:page_id/types/:type_id/like/new(.:format)      types/like#new
 edit_user_catalog_page_type_like GET     /users/:user_id/catalogs/:catalog_id/pages/:page_id/types/:type_id/like/:id/edit(.:format) types/like#edit
      user_catalog_page_type_like GET         /users/:user_id/catalogs/:catalog_id/pages/:page_id/types/:type_id/like/:id(.:format)      types/like#show
                                  PATCH  /users/:user_id/catalogs/:catalog_id/pages/:page_id/types/:type_id/like/:id(.:format)      types/like#update
                                  PUT    /users/:user_id/catalogs/:catalog_id/pages/:page_id/types/:type_id/like/:id(.:format)      types/like#update
                                  DELETE /users/:user_id/catalogs/:catalog_id/pages/:page_id/types/:type_id/like/:id(.:format)      types/like#destroy

The link is not working, have any idea?

Thank you!

Hey Pablo,

Your link looks correct and so do your routes. Are you getting an error?

Oh of course. If you look at your routes, there is no POST paths there. I think you can change the resources :like to be singular instead like this: resource :like and that will help.

In either case, your rake routes should be creating a POST link to let you create a new like. It's odd that your current one doesn't have a POST route.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,674+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.