繁体   English   中英

Rails使用嵌套资源路由错误

[英]Rails routing error with nested resources

在我的应用程序中,我有一个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路由,我在输出中找到以下路由:

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"

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM