开发者

Rails routing error with nested resources

开发者 https://www.devze.com 2023-01-23 06:55 出处:网络
In my application, I have a RecipesController and a CommentsController. All comments belong to a recipe, and can be voted up. Here\'s a snippet from my routes.rb:

In my application, I have a RecipesController and a CommentsController. All comments belong to a recipe, and can be voted up. Here's 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, I find the following route in the output:

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

The CommentsController has a method called vote_up.

Also, linking to the route works (from my view)

    <%= link_to 'Vote up', vote_up_recipe_comment_path(@recipe, comment), 开发者_如何学Go:method => 'put' %> <br />

However, clicking on that link gives me the following error:

Routing Error

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

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


I think that you get this error message because the request is made via HTTP GET method, not PUT.

In order to create links that use POST/PUT/DELETE method, your application should correctly load a Javascript Rails adapter.

Check that your app has jQuery (http://github.com/rails/jquery-ujs) or Prototype JS adapter and that your layout correctly loads it.


try the following tweak: send the put method as a symbol

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

0

精彩评论

暂无评论...
验证码 换一张
取 消