Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link_to update (without form)

Tags:

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

Routes:

resources :users do   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 when I click the link, it tries to go into the show action.

What is the right way to do this?

like image 591
Joe Avatar asked Jan 20 '11 19:01

Joe


1 Answers

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.

like image 173
Jeppe Liisberg Avatar answered Oct 08 '22 06:10

Jeppe Liisberg