link_to update (without form)

独自空忆成欢 提交于 2019-11-30 21:42:37

问题


I want a link to update a resource, without using an HTML form.

Routes :

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

Rake routes:

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

I want to use the put to update a friend by a simple link, something like this:
<%=link_to "Add as friend", user_friend_path(current_user, :method=>'put')%>

But as Rails meet that link he tries to go into the show action.
The question is: what is the right link to do that?


回答1:


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

Will insert a link with attribute 'data-method' set to 'put', which will in turn be picked up by the rails javascript and turned into a form behind the scenes... I guess that's what you want.

You should consider using :post, since you are creating a new link between the two users, not updating it, it seems.



来源:https://stackoverflow.com/questions/4751869/link-to-update-without-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!