Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DELETE routes are failing #42

Closed
willian opened this issue Jun 4, 2015 · 14 comments
Closed

DELETE routes are failing #42

willian opened this issue Jun 4, 2015 · 14 comments

Comments

@willian
Copy link

willian commented Jun 4, 2015

Every time I tried to remove a message or clear all the messages I got this error:
error1

At my router.rb I added:

if Rails.env.development?
  mount LetterOpenerWeb::Engine, at: '/letter_opener'
end

And this is what I get when I run rake routes:

              letter_opener_web        /letter_opener                              LetterOpenerWeb::Engine
                    sidekiq_web        /sidekiq                                    Sidekiq::Web
                           root GET    /                                           redirect(301, http://gridleague-dev.com)

Routes for LetterOpenerWeb::Engine:
clear_letters DELETE /clear(.:format)                 letter_opener_web/letters#clear
delete_letter DELETE /:id(.:format)                   letter_opener_web/letters#destroy
      letters GET    /                                letter_opener_web/letters#index
       letter GET    /:id(/:style)(.:format)          letter_opener_web/letters#show
              GET    /:id/attachments/:file(.:format) letter_opener_web/letters#attachment

For some reason it calls using POST but should use DELETE instead.

@mmontalvo
Copy link

Which version are you using? Working fine on 1.2.3

@willian
Copy link
Author

willian commented Jul 30, 2015

@mmontalvo thank you for your answer, I'm using 1.3.0:

$ bundle list | ag letter
  * letter_opener (1.3.0)
  * letter_opener_web (1.3.0)

@mmontalvo
Copy link

I switched to 1.3.0 and still working. Which browser are you using? Can you please paste some logs?

@willian
Copy link
Author

willian commented Jul 30, 2015

That failed on Chrome and Safari, the log says:

Started POST "/letter_opener/1438089424_07b115c" for 127.0.0.1 at 2015-07-30 14:02:32 -0300
[Rollbar] Reporting exception: No route matches [POST] "/letter_opener/1438089424_07b115c"
[Rollbar] Scheduling payload
[Rollbar] Sending payload
[Rollbar] Got unexpected status code from Rollbar api: 400
[Rollbar] Response: {
  "err": 1,
  "message": "access token required"
}
[Rollbar] Details: https://rollbar.com/instance/uuid?uuid=f8da38ee-a731-4d42-b092-f9eac380051b (only available if report was successful)
[Rollbar] Exception uuid saved in env: f8da38ee-a731-4d42-b092-f9eac380051b

ActionController::RoutingError (No route matches [POST] "/letter_opener/1438089424_07b115c"):
  .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  .bundle/ruby/2.2.0/gems/rollbar-1.5.3/lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:38:in `call_app'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `block in call'
  .bundle/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `block in tagged'
  .bundle/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:26:in `tagged'
  .bundle/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/tagged_logging.rb:68:in `tagged'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/rack/logger.rb:20:in `call'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/request_id.rb:21:in `call'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/rack-1.6.0/lib/rack/runtime.rb:18:in `call'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/activesupport-4.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/rack-1.6.0/lib/rack/lock.rb:17:in `call'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/static.rb:113:in `call'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/rack-timeout-0.2.4/lib/rack/timeout.rb:108:in `call'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/engine.rb:518:in `call'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/application.rb:164:in `call'
  .bundle/ruby/2.2.0/gems/newrelic_rpm-3.12.1.298/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
  .bundle/ruby/2.2.0/gems/rack-1.6.0/lib/rack/content_length.rb:15:in `call'
  .bundle/ruby/2.2.0/gems/thin-1.6.3/lib/thin/connection.rb:86:in `block in pre_process'
  .bundle/ruby/2.2.0/gems/thin-1.6.3/lib/thin/connection.rb:84:in `catch'
  .bundle/ruby/2.2.0/gems/thin-1.6.3/lib/thin/connection.rb:84:in `pre_process'
  .bundle/ruby/2.2.0/gems/thin-1.6.3/lib/thin/connection.rb:53:in `process'
  .bundle/ruby/2.2.0/gems/thin-1.6.3/lib/thin/connection.rb:39:in `receive_data'
  .bundle/ruby/2.2.0/gems/eventmachine-1.0.7/lib/eventmachine.rb:187:in `run_machine'
  .bundle/ruby/2.2.0/gems/eventmachine-1.0.7/lib/eventmachine.rb:187:in `run'
  .bundle/ruby/2.2.0/gems/thin-1.6.3/lib/thin/backends/base.rb:73:in `start'
  .bundle/ruby/2.2.0/gems/thin-1.6.3/lib/thin/server.rb:162:in `start'
  .bundle/ruby/2.2.0/gems/rack-1.6.0/lib/rack/handler/thin.rb:19:in `run'
  .bundle/ruby/2.2.0/gems/rack-1.6.0/lib/rack/server.rb:286:in `start'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/commands/server.rb:80:in `start'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:80:in `block in server'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:75:in `tap'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:75:in `server'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
  .bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
  /bin/rails:4:in `require'
  /bin/rails:4:in `<main>'


  Rendered .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.4ms)
  Rendered .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_route.html.erb (16.2ms)
  Rendered .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_route.html.erb (3.8ms)
  Rendered .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/routes/_table.html.erb (31.2ms)
  Rendered .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.6ms)
  Rendered .bundle/ruby/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (435.0ms)

https://www.dropbox.com/s/hnqn8dsa3hp5bar/screencapture-api-gridleague-dev-com-letter_opener-1438089424_07b115c-1438275814641.png?dl=0

@snop-snov
Copy link

I have this problem too. Are there any changes?
I tried gem versions 1.2.3, 1.3.1 and got error in both cases.

@ihatov08
Copy link
Contributor

api/config/initializers/monkey_patches/letter_opener_web.rb

LetterOpenerWeb::Engine.routes.append do
  post 'clear'                 => 'letters#clear'
  post ':id'                   => 'letters#destroy'
end

@pseudomuto
Copy link
Contributor

Is this still an issue with master?

@Slike9
Copy link

Slike9 commented Aug 18, 2018

Adding Rack::MethodOverride middleware to my application helped with the problem:

# config/environments/development.rb
Rails.application.configure do
  # ...
  config.middleware.use Rack::MethodOverride
end

About Rack::MethodOverride from https://guides.rubyonrails.org/rails_on_rack.html:

Allows the method to be overridden if params[:_method] is set. This is the middleware which supports the PUT and DELETE HTTP method types.

An API application is not equipped with it - https://edgeguides.rubyonrails.org/api_app.html#choosing-middleware.

@tgaff
Copy link

tgaff commented Aug 30, 2019

Adding Rack::MethodOverride middleware to my application helped with the problem:

About `Rack::MethodOverride` from https://guides.rubyonrails.org/rails_on_rack.html:

This was exactly the problem I was facing. I didn't want to pull in that middleware just for a test gem though so I worked around it adding post routes for letters#destroy and letters#clear: 793b4e8

@rwxdash
Copy link

rwxdash commented Sep 6, 2019

Adding Rack::MethodOverride middleware to my application helped with the problem:

About `Rack::MethodOverride` from https://guides.rubyonrails.org/rails_on_rack.html:

This was exactly the problem I was facing. I didn't want to pull in that middleware just for a test gem though so I worked around it adding post routes for letters#destroy and letters#clear: 793b4e8

This solution probably works but this is still an issue since the button for clear and destroy on UI is sending a POST request instead of DELETE in the version 1.3.4.

Version:

➜  bundle list | grep letter
  * letter_opener (1.7.0)
  * letter_opener_web (1.3.4)

@tgaff
Copy link

tgaff commented Oct 9, 2019

This solution probably works but this is still an issue since the button for clear and destroy on UI is sending a POST request instead of DELETE in the version 1.3.4.

That's fairly normal - web browsers don't support sending a real DELETE from a form - only GET and POST. That's why we have the Rack::MethodOverride middleware. Sending a POST is normal here, it's just that usually with that middleware it is correctly translated back to DELETE on the server-side.

@fgrehm
Copy link
Owner

fgrehm commented Feb 18, 2020

Sorry for the silence here, I might work on this soon #69 (comment)

@hassan84-PK
Copy link

Use LetterOpenerWeb::Letter.destroy_all from rails c untill this bug is fixed.

@fgrehm
Copy link
Owner

fgrehm commented Nov 6, 2021

Hey peeps, as of the 2.0 release that just went out this should no longer be a problem since we now use POST for everything

LetterOpenerWeb::Engine.routes.draw do
get '/' => 'letters#index', as: :letters
post 'clear' => 'letters#clear', as: :clear_letters
get ':id(/:style)' => 'letters#show', as: :letter
post ':id/delete' => 'letters#destroy', as: :delete_letter
get ':id/attachments/:file' => 'letters#attachment'
end

@fgrehm fgrehm closed this as completed Nov 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants