Reputation: 299
I know this question was asked a lot of times, but none of the recipes helped me so far.
I have an application written in Ruby 2.0.0, Rails 4.0.0 with following routes.rb:
resources :announcements do
end
Which generates the following routes:
announcements GET /announcements(.:format) announcements#index
POST /announcements(.:format) announcements#create
new_announcement GET /announcements/new(.:format) announcements#new
edit_announcement GET /announcements/:id/edit(.:format) announcements#edit
announcement GET /announcements/:id(.:format) announcements#show
PATCH /announcements/:id(.:format) announcements#update
PUT /announcements/:id(.:format) announcements#update
DELETE /announcements/:id(.:format) announcements#destroy
Then in HTML I use very simple construct:
<%= button_to 'X', announcement, method: :delete %>
Which produces the following code:
<form method="post" class="button_to" action="/announcements/525c0d28b9fd0171e2000003">
<div>
<input type="hidden" value="delete" name="_method">
<input type="submit" value="X">
<input type="hidden" value="DR+QjuAG9npqqWRV16Zq0G4Cq3lGpNHY0qjV8LJpUZo=" name="authenticity_token">
</div>
</form>
Everything looks pretty simple, clean and correct. Nevertheless I'm getting "No Route Matches" error. Why?
Upvotes: 1
Views: 1686
Reputation: 299
The problem was in missing middleware. I have added
config.middleware.use Rack::MethodOverride
to /config/application.rb
Upvotes: 4
Reputation: 6574
Try
<%= button_to "Delete", {:controller => :announcements, :action => 'destroy', :id => announcement.id }, :method => :delete %>
Upvotes: 0
Reputation: 76784
That's because the routes for those paths need to have :method => :delete
(delete path) & :method => :put
(update path). POST won't work for either
As per the Rails Documentation:
In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database
You also need to use announcement_path
for your URL helper
Upvotes: -1