2010-11-02 114 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 %>