代码之家  ›  专栏  ›  技术社区  ›  Chris Eidhof

嵌套资源的Rails路由错误

  •  3
  • Chris Eidhof  · 技术社区  · 16 年前

    在我的应用程序中,我有一个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
    

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

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

    CommentsController有一个名为“投票”的方法。

    此外,链接到路线工程(从我的视图)

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

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

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

    我错过了什么?我不知道如何调试它,因为据我所见,路由应该匹配。

    2 回复  |  直到 16 年前
        1
  •  5
  •   Nicolas Blanco    16 年前

    我认为您收到这个错误消息是因为请求是通过HTTP GET方法发出的,而不是PUT。

    为了创建使用post/put/delete方法的链接,应用程序应该正确地加载一个javascript Rails适配器。

    检查您的应用程序是否有jquery(http://github.com/rails/jquery-ujs)或原型JS适配器,以及您的布局是否正确地加载了它。

        2
  •  3
  •   Jed Schneider    16 年前

    尝试以下调整:将Put方法作为符号发送

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