简体   繁体   中英

Rails 3 : Can't get form_for to work as a 'delete' following the RESTful achitecture => always giving a ROUTING ERROR

I have a very simple render that goes as follow:

<%= form_for(:relationships, :url => relationships_path, :html => {:method => 'delete'}) do |f| %>
<div><%= f.hidden_field :user_id_to_unfollow, :value => @user.id %></div>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>

When I submit this form it will always give me a

Routing Error
No route matches "/relationships"

on my page.

In my relationships controller, I have created all the propers methods:

def create    
...
end

def destroy    
...
end

def update    
...
end

def show    
...
end

And in my routes config I have made sure to allow all routes for the relationships controller

resources :relationships

But I can't seem to get into the destroy method of the controller :(

However if I remove the

:html => {:method => 'delete'}

method parameter in the form_for then I get to the create method of the controller no pb.

I don't get it....

Alex

ps: this is the rake routes results for relationships:

relationships GET    /relationships(.:format)          {:action=>"index", :controller=>"relationships"}
              POST   /relationships(.:format)          {:action=>"create", :controller=>"relationships"}

You should point the delete request to single resource url eg. relationships/4325 . Run rake routes to view what url/verb combinations are valid.

--edit

Routes for relationship resources:

resources :relationships, :only => [:index, :create, :destroy]

Unfollow button (creates a form for itself):

= button_to "Unfollow", relationship_path(relationship), :method => 'delete'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM