Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: ":method => :post" doesn't work... seems to be 'GET' when it should 'POST'

I'm trying to implement the "Friendship" in my Rails 3 app as described in Railscast 163:Self Referential Assosication

I have everything set up as described. I am using a basic user model that logis in with Authlogic which works fine. However when I try to add a friend using the following link:

<% for user in @users %>   
 <%=h user.username %> 
  <%= link_to "Add Friend", friendships_path(:friend_id => user), :method => :post %>     
<% end %>

I get a redirect to http://localhost:3000/friendships?friend_id=2 and a Unknown action The action 'index' could not be found for FriendshipsController error with no further explanation. This is expecially strange since I have a hard coded redirect back to the "User#show" method for my current user (i.e. redirect back to profile after adding friend).

If it helps, here is my "friendships#create" method:

def create  
  @friendship = current_user.friendships.build(:friend_id => params[:friend_id])  
  if @friendship.save  
    flash[:notice] = "Added friend."  
    redirect_to :controller => 'users', :action => 'show', :id =>'current_user'
  else  
    flash[:notice] = "Unable to add friend."  
    redirect_to :controller => 'users', :action => 'show', :id =>'current_user' 
  end  
end

Any idea what could be causing this? I found someone having a similar problem here but I couldn't find a definite fix: Rails 3 and Friendship Models

Thanks in advance for your help!

~Dan

like image 723
thoughtpunch Avatar asked Jan 25 '11 22:01

thoughtpunch


2 Answers

I think that link_to put the arguments into the query string as it is creating with html link even if you put :method => :post if js is disabled.

you could simulte a post with javascript :onclik event.

aniway , use a link_to with method :post is generaly a bad idea. in rails you could use button_to helper for this pourpose and style it like a link.

edit:
In Rails3 doc seems that link_to have to simulate the same behaviur of button_to when called with params :method=>:post

(dynamically create an HTML form and immediately submit the form ).

but it's not true for me in Rails 3.0.3 even if javascript is enabled. I will investigate.

anyway you should be using buttons and forms for anything that isn't a GET; hyperlinks intentionally don't allow for methods other than GET

edit2: ok, rails3 don't create a inline form for simulate the post request via link. in Rails3 a data-method=post attribute is added to the tag for manipulate it via javascript function. this way the request gracefully degradate in a get call if js is disabled.

like image 114
andrea Avatar answered Sep 18 '22 01:09

andrea


It's a late answer but it's a solution to this problem (at least it works for me):

<%= link_to "Add Friend", {:controller => :friendships, :action => :create, :friend_id => user_id }, :method => :post %>     

I know it's long overdue for your problem, but it may help someone :)

like image 28
Zippie Avatar answered Sep 21 '22 01:09

Zippie