Rails使用嵌套资源路由错误

Chr*_*hof 3 routes ruby-on-rails

在我的应用程序中,我有一个RecipesController和一个CommentsController.所有评论都属于食谱,可以投票.这是我的routes.rb的片段:

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

    resources :comments do
      member do
        put 'vote_up'
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

如果我运行rake路由,我在输出中找到以下路由:

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

CommentsController有一个名为vote_up的方法.

此外,链接到路线工作(从我看来)

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

但是,单击该链接会出现以下错误:

Routing Error

No route matches "/recipes/7/comments/4/vote_up"
Run Code Online (Sandbox Code Playgroud)

我错过了什么?我不知道如何调试这个,因为据我所知,路线应该匹配.

Nic*_*nco 5

我认为您收到此错误消息,因为请求是通过HTTP GET方法,而不是PUT.

为了创建使用POST/PUT/DELETE方法的链接,您的应用程序应该正确加载Javascript Rails适配器.

检查您的应用程序是否具有jQuery(http://github.com/rails/jquery-ujs)或Prototype JS适配器,并确保您的布局正确加载它.