2011-01-20 85 views
17

我想要一个链接来更新资源,而不使用HTML表单。link_to update(without form)

路线:

resources :users do 
resource :profile, :controller=>"profiles" 
resources :friends 
end  

耙路线:

user_friend GET /users/:user_id/friends/:id(.:format){:action=>"show", :controller=>"friends"} 
      PUT /users/:user_id/friends/:id(.:format){:action=>"update", :controller=>"friends"} 

我想使用put通过一个简单的链接,这样的更新的朋友:
< %= link_to“添加为朋友”,user_friend_path(current_user,:method =>'put')%>

但是,当Rails遇见那个链接时,他试图进入节目的行动。
问题是:做这件事的正确环节是什么?

+0

您是否尝试添加动作参数? <%= link_to“添加为朋友”,user_friend_path(current_user,:method =>'put,:action =>:update')%>如果您没有传递任何更新参数,为什么还需要put方法? – 2011-01-20 20:07:09

回答

31
link_to "Add as friend", user_friend_path(current_user, @friend), :method=> :put 

将插入具有属性“数据法”设置为“放”,这将反过来由JavaScript,变成了幕后的形式来进行的轨道拿起一个链接...我想这是你想要什么。

您应该考虑使用:帖子,因为你正在创建一个新的链接在两个用户之间,不更新它,它似乎。

+0

我在这个视图中没有@friend,我只有它的ID,它没有工作。 – Joe 2011-01-20 20:18:34