2

我正在嘗試開發一個允許用戶對鏈接進行投票的reddit樣式的網站。投票功能超出了resources :links支持的基本CRUD,我已經編寫了向上/向下操作並將它們鏈接到視圖,但我不知道如何處理路由。有人可以演示我將如何路由到自定義控制器操作?下面附上了我的文件。由於自定義控制器操作的路由

當我加載視圖

No route matches {:controller=>"links", :action=>"up"} 

連接控制器https://gist.github.com/1272577

查看https://gist.github.com/1272580

路線我收到此錯誤https://gist.github.com/1272584

回答

2

您可以擴展這個例子resources映射:

resources :links do 
    member do 
    match :up 
    match :down 
    end 
end 

這些操作必須在LinksController類中可用(與new,create ...相同)。 更多內容Ruby on Rails Guide: Rails Routing

提示:改變實體狀態的動作不應該使用GET動詞。這是因爲搜索漫遊器或加速器可能會跟隨您的投票鏈接。 你的鏈接應該是:

<%= link_to "+", up_link_path, :method => :post, :rel => 'nofollow' %> 

並在控制器應該只修改裏面if request.post?您entinty。你應該仍然支持GET,不會導致404s。

+0

我更新了路線文件並運行'耙路線',但此解決方案無法正常工作。我也用'put'替換'post',因爲我試圖更新屬性,但是也沒有解決它。 – Dru

+0

我將'post'更改爲'put',並且修改了我的視圖鏈接'<%= link_to'+',up_link_url(link),:method =>:put%><%= link.points%>'Thanks for the幫幫我。 – Dru

相關問題