Rails routing error with nested resources

In my application, I have a RecipesController and commentController. All comments belong to the recipe and can be voted. Here is a snippet from my routes. Rb:

  resources :recipes do
    member do
      put 'vote_up'
      post 'comment'
    end

    resources :comments do
      member do
        put 'vote_up'
      end
    end
  end

If I run rake routes, on the output I find the following route:

vote_up_recipe_comment PUT    /recipes/:recipe_id/comments/:id/vote_up(.:format) {:action=>"vote_up", :controller=>"comments"}

CommentsController has a method called vote_up.

Also, the route link works (from my view)

    <%= link_to 'Vote up', vote_up_recipe_comment_path(@recipe, comment), :method => 'put' %> <br />

However, when you click this link, the following error appears:

Routing Error

No route matches "/recipes/7/comments/4/vote_up"

What am I missing? I am not sure how to debug this because, as far as I can see, the route should fit.

+3
source share
2 answers

, , HTTP GET, PUT.

, POST/PUT/DELETE, Javascript Rails.

, jQuery (http://github.com/rails/jquery-ujs) Prototype JS .

+5

: put

<%= link_to 'Vote up', vote_up_recipe_comment_path(@recipe, comment), :method => :put %>

+3

Source: https://habr.com/ru/post/1772622/


All Articles