2011-01-30 41 views
0

我在RoR網站內建立友誼。它的模型是user_id,friend_id和pending(布爾)。我大部分時間都是關於RailsCast的朋友關係,但也做了一些改變。我至今是,當你去一個用戶的頁面,您可以點擊請求友誼和代碼使用:軌道上的紅寶石沒有路線匹配動作:刪除

user_friendships_path(current_user, :friend_id => @user), :method => :post 

這就需要在友誼控制器的創建方法。它會自動將pending設置爲true。我現在想要的是有一個接受它的鏈接,這將只是等待爲false。所以,我想設置它像

(<%= link_to "Accept", user_friendship_path(:user_id => current_user.id, :friend_id => friendship.user.id, :pending => 'false'), :method => :put %>) 

我其實不願意去編輯頁面,因爲它只是需要的是布爾值設置爲false,所以我想直接調用更新。但當我運行此頁時,出現錯誤:

No route matches {:action=>"destroy", :controller=>"friendships", :user_id=>1, :friend_id=>2, :pending=>"false"} 

我不明白爲什麼。我沒有調用destroy(這將是:method =>:delete),並且友誼控制器中實際上有一個銷燬方法。

的資源設置,如:

resources :users do 
    resources :friendships 
end 

而且從運行 「耙路線」 的路徑是:

user_friendships GET /users/:user_id/friendships(.:format)    {:action=>"index", :controller=>"friendships"} 
user_friendships POST /users/:user_id/friendships(.:format)    {:action=>"create", :controller=>"friendships"} 
new_user_friendship GET /users/:user_id/friendships/new(.:format)   {:action=>"new", :controller=>"friendships"} 
edit_user_friendship GET /users/:user_id/friendships/:id/edit(.:format) {:action=>"edit", :controller=>"friendships"} 
user_friendship GET /users/:user_id/friendships/:id(.:format)   {:action=>"show", :controller=>"friendships"} 
user_friendship PUT /users/:user_id/friendships/:id(.:format)   {:action=>"update", :controller=>"friendships"} 
user_friendship DELETE /users/:user_id/friendships/:id(.:format)   {:action=>"destroy", :controller=>"friendships"} 

任何幫助將不勝感激。如果您需要更多信息,請告訴我。

謝謝。

+0

如果您嘗試:link_to「Accept」,user_friendship_path(:user_id => current_user.id,:friend_id => friendship.user.id),:method =>:put%>)會怎麼樣?其實,你不需要發送待處理的參數,這種安全漏洞 – apneadiving 2011-01-30 23:57:21

回答

2

,根據耙路線存在的路徑是

user_friendship PUT /users/:user_id/friendships/:id(.:format)   {:action=>"update", :controller=>"friendships"} 

您正在使用放的方法,但你不提供「:ID」。

有這取決於你正在嘗試做的兩個解決方案:

  1. 變化的方法來獲得 - 這將指向你新的動作
  2. 地址:ID到您的網址構建器 - 將指向你的更新動作

你的代碼傾向於第二,但我認爲如果你想創建一個新的友誼,第一個會更好。

+0

就是這樣!哈哇。後見之明。很顯然,我需要給它一個特定的ID來更新。謝謝! – user596142 2011-02-01 02:11:35

0

我很困惑爲什麼URL助手映射到銷燬行爲。然而存在一個更簡單的問題:user_friendship_path期望參數:user_id:id,而不是:friend_id