2010-11-02 112 views
3

在我的申请,我有一个的RecipesController和CommentsController。所有评论都属于一个配方,可以投票。下面是从我的routes.rb片段:Rails的路由错误与嵌套资源

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

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

如果我运行耙路线,我发现在输出以下路线:

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

的CommentsController有一个名为vote_up方法。

此外,链接到路线的作品(从我的观点)

<%= 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" 

我缺少什么?我不知道如何调试,因为据我看到的路线应该匹配。

回答

5

我认为你收到此错误消息,因为该请求是通过HTTP GET方法制成的,不能放。

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

检查您的应用程序具有的jQuery(http://github.com/rails/jquery-ujs)或原型JS适配器和布局正确加载它。

+0

非常感谢,该诀窍。我确实删除了一些javascript文件。事后看来,这很明显,但它可能会花费我很长时间才能找到。 – 2010-11-02 10:54:17

3

尝试以下的调整:发送put方法为标志

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