2011-05-20 44 views
0

我有一個HAML鏈接,看起來像這樣Rails 3中問題的路線

=link_to "accept", friendship, :method => :put 

,我試圖映射「把」使用「更新」行動在我的控制器

在我的路線

像這樣

resources :friendships do 
    collection do 
    get :create 
    delete :destroy 
    put :update 
    end 
end 

所以如果我使用方法「搞定」它應該得到一個新朋友(的作品),如果我刪除,則「消滅」用於行動,如果我把被使用的「更新」行動。

我認爲我是以某種方式完全錯誤的。

我的「創造」朋友鏈接看起來像這樣

=link_to "Add Friend", friendships_path(:friend_id => provider), :method => :post 

,這是我的整個friends.haml

.profile 
    .providers 
    %h1 Other Users 
    - @providers.each do |provider| 
     %p 
     =provider.login 
     =link_to "Add Friend", friendships_path(:friend_id => provider), :method =>   
:post 
    .friends 
    %h1 Friends 
    - @friends.each do |friendship| 
     %p 
     =friendship.friend.login 
     =link_to "remove", friendship, :method => :delete 
     =link_to "message", memos_path(:other_user => friendship.friend) 

    .friends-out 
    %h1 Friends Out 
    - @friends_out.each do |friendship| 
     %p 
     =friendship.friend.login 
     =link_to "remove", friendship, :method => :delete 

    .friends-in 
    %h1 Friends In 
    - @friends_in.each do |friendship| 
     %p 
     =friendship.friend.login 
     =link_to "remove", friendship, :method => :delete 
     =link_to "accept", friendship, :method => :put 

回答

0

呀,你幾乎做對的,但你做了錯誤辦法。你應該這樣做:

resources :friendships 

沒有更多的也不少,僅此而已。因爲在使用資源時,默認情況下會將put協議映射到update操作,它會將delete協議映射到destroy操作,並將POST協議映射到create操作。因爲在數據庫以某種方式更改(創建,更新,銷燬)時不應使用GET。

而當你使用的link_to助手,嘗試這樣的:

=link_to "accept", friendship_path(@friendship), :method => :put 

編輯:

另外,還要確保你的JavaScript包括處理髮布的指定方法的默認:

<%= javascript_include_tag :defaults %> 
<%= csrf_meta_tag %> 
+0

<%= link_to「接受」,友誼,:方法=>:put%>將工作,因爲我認爲友誼是同一個班級的對象。 (簡而言之,我認爲他正在運行一個循環) – 2011-05-20 11:22:09

+0

是的,我在一個朋友的圈子裏,對不起,我把那個細節留下了 – 2011-05-20 11:24:37

+0

是的,如果友誼是實例,那當然會起作用。 – DanneManne 2011-05-20 11:24:38