Rails 4 link_to url causes browser to GET when it should DELETE

你离开我真会死。 提交于 2020-01-17 04:14:06

问题


Searching around, I see that variations on this question used to be asked on StackOverflow fairly often, but nothing in recent years. That makes me wonder if perhaps I have something that is out of date, perhaps a Javascript library. We just started working on a new Rails 4 site, using Ruby 2, however we accepted the frontend code from our designer, and we used it mostly untouched. That includes:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>    

We have a log out link:

<%= link_to('LOG OUT', main_app.destroy_user_session_path, :method => :delete) %>  

Sadly, when we click on this, the browser does a GET instead of a DELETE:

Started GET "/users/sign_out" for 127.0.0.1 at 2015-03-19 17:33:09 -0400
Processing by Refinery::PagesController#show as HTML
  Parameters: {"path"=>"users/sign_out", "locale"=>:en}
  Refinery::Page Load (0.5ms)  SELECT  "refinery_pages".* FROM "refinery_pages" INNER JOIN "refinery_page_translations" ON "refinery_page_translations"."refinery_page_id" = "refinery_pages"."id" WHERE "refinery_pages"."parent_id" IS NULL AND "refinery_page_translations"."locale" IN ('en') AND "refinery_page_translations"."slug" = 'users'  ORDER BY "refinery_pages"."id" ASC LIMIT 1
  Refinery::Page Load (0.4ms)  SELECT  "refinery_pages".* FROM "refinery_pages"  WHERE "refinery_pages"."menu_match" = '^/404$'  ORDER BY "refinery_pages"."id" ASC LIMIT 1
  Refinery::PagePart Load (0.7ms)  SELECT "refinery_page_parts".* FROM "refinery_page_parts"  WHERE "refinery_page_parts"."refinery_page_id" IN (2)  ORDER BY position ASC
  Refinery::PagePart::Translation Load (0.4ms)  SELECT "refinery_page_part_translations".* FROM "refinery_page_part_translations"  WHERE "refinery_page_part_translations"."refinery_page_part_id" IN (3)
  Rendered refinery/pages/show.html.erb within layouts/application (2.6ms)
  Rendered vendor/bundle/ruby/2.1.0/bundler/gems/refinerycms-11f8d1eeb45e/core/app/views/refinery/_html_tag.html.erb (0.1ms)
  Rendered refinery/_site_bar.html.erb (0.8ms)
  Rendered _login.html.erb (0.1ms)
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
Filter chain halted as :find_page rendered or redirected
Completed 404 Not Found in 84ms (Views: 69.7ms | ActiveRecord: 2.2ms)

This could also be a conflict with RefineryCSM, which we are using for some of our site.

These are all the routes in our system:

 bundle exec rake routes
                  Prefix Verb   URI Pattern                    Controller#Action
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                    root GET    /                              intro#join
      authenticated_root GET    /                              member#profile
                benefits GET    /intro/benefits(.:format)      intro#benefits
                    join GET    /intro/join(.:format)          intro#join
                  search GET    /member/search(.:format)       member#search
                 library GET    /member/library(.:format)      member#library
                progress GET    /member/progress(.:format)     member#progress
                 profile GET    /member/profile(.:format)      member#profile
                refinery        /                              Refinery::Core::Engine

Routes for Refinery::Core::Engine:
                       videos_videos GET    /videos(.:format)                           refinery/videos/videos#index
                        videos_video GET    /videos/:id(.:format)                       refinery/videos/videos#show
update_positions_videos_admin_videos POST   /refinery/videos/update_positions(.:format) refinery/videos/admin/videos#update_positions
                 videos_admin_videos GET    /refinery/videos(.:format)                  refinery/videos/admin/videos#index
                                     POST   /refinery/videos(.:format)                  refinery/videos/admin/videos#create
              new_videos_admin_video GET    /refinery/videos/new(.:format)              refinery/videos/admin/videos#new
             edit_videos_admin_video GET    /refinery/videos/:id/edit(.:format)         refinery/videos/admin/videos#edit
                  videos_admin_video PATCH  /refinery/videos/:id(.:format)              refinery/videos/admin/videos#update
                                     PUT    /refinery/videos/:id(.:format)              refinery/videos/admin/videos#update
                                     DELETE /refinery/videos/:id(.:format)              refinery/videos/admin/videos#destroy
                           wymiframe GET    /wymiframe(/:id)(.:format)                  refinery/fast#wymiframe
                                root GET    /                                           refinery/pages#home
                                page GET    /pages/:id(.:format)                        refinery/pages#show
           pages_admin_preview_pages POST   /refinery/pages/preview(.:format)           refinery/pages/admin/preview#show
            pages_admin_preview_page PATCH  /refinery/pages/preview/*path(.:format)     refinery/pages/admin/preview#show
                     admin_edit_page GET    /refinery/pages/*path/edit(.:format)        refinery/admin/pages#edit
                admin_children_pages GET    /refinery/pages/*path/children(.:format)    refinery/admin/pages#children
                   admin_update_page PATCH  /refinery/pages/*path(.:format)             refinery/admin/pages#update
                   admin_delete_page DELETE /refinery/pages/*path(.:format)             refinery/admin/pages#destroy
        update_positions_admin_pages POST   /refinery/pages/update_positions(.:format)  refinery/admin/pages#update_positions
                         admin_pages GET    /refinery/pages(.:format)                   refinery/admin/pages#index
                                     POST   /refinery/pages(.:format)                   refinery/admin/pages#create
                      new_admin_page GET    /refinery/pages/new(.:format)               refinery/admin/pages#new
                     edit_admin_page GET    /refinery/pages/:id/edit(.:format)          refinery/admin/pages#edit
                          admin_page PATCH  /refinery/pages/:id(.:format)               refinery/admin/pages#update
                                     PUT    /refinery/pages/:id(.:format)               refinery/admin/pages#update
                                     DELETE /refinery/pages/:id(.:format)               refinery/admin/pages#destroy
         link_to_admin_pages_dialogs GET    /refinery/pages_dialogs/link_to(.:format)   refinery/admin/pages_dialogs#link_to
                    admin_page_parts POST   /refinery/page_parts(.:format)              refinery/admin/page_parts#create
                 new_admin_page_part GET    /refinery/page_parts/new(.:format)          refinery/admin/page_parts#new
                     admin_page_part DELETE /refinery/page_parts/:id(.:format)          refinery/admin/page_parts#destroy
                                     GET    /system/resources/*dragonfly(.:format)      <Dragonfly::App name=:refinery_resources >
              insert_admin_resources GET    /refinery/resources/insert(.:format)        refinery/admin/resources#insert
                     admin_resources GET    /refinery/resources(.:format)               refinery/admin/resources#index
                                     POST   /refinery/resources(.:format)               refinery/admin/resources#create
                  new_admin_resource GET    /refinery/resources/new(.:format)           refinery/admin/resources#new
                 edit_admin_resource GET    /refinery/resources/:id/edit(.:format)      refinery/admin/resources#edit
                      admin_resource PATCH  /refinery/resources/:id(.:format)           refinery/admin/resources#update
                                     PUT    /refinery/resources/:id(.:format)           refinery/admin/resources#update
                                     DELETE /refinery/resources/:id(.:format)           refinery/admin/resources#destroy
                                     GET    /system/images/*dragonfly(.:format)         <Dragonfly::App name=:refinery_images >
                 insert_admin_images GET    /refinery/images/insert(.:format)           refinery/admin/images#insert
                        admin_images GET    /refinery/images(.:format)                  refinery/admin/images#index
                                     POST   /refinery/images(.:format)                  refinery/admin/images#create
                     new_admin_image GET    /refinery/images/new(.:format)              refinery/admin/images#new
                    edit_admin_image GET    /refinery/images/:id/edit(.:format)         refinery/admin/images#edit
                         admin_image PATCH  /refinery/images/:id(.:format)              refinery/admin/images#update
                                     PUT    /refinery/images/:id(.:format)              refinery/admin/images#update
                                     DELETE /refinery/images/:id(.:format)              refinery/admin/images#destroy
                               login GET    /refinery/login(.:format)                   refinery/sessions#new
                              logout GET    /refinery/logout(.:format)                  refinery/sessions#destroy
                          new_signup GET    /refinery/users/register(.:format)          refinery/users#new
                              signup POST   /refinery/users/register(.:format)          refinery/users#create
           new_refinery_user_session GET    /refinery/users/login(.:format)             refinery/sessions#new
               refinery_user_session POST   /refinery/users/login(.:format)             refinery/sessions#create
       destroy_refinery_user_session DELETE /refinery/users/logout(.:format)            refinery/sessions#destroy
              refinery_user_password POST   /refinery/users/password(.:format)          refinery/passwords#create
          new_refinery_user_password GET    /refinery/users/password/new(.:format)      refinery/passwords#new
         edit_refinery_user_password GET    /refinery/users/password/edit(.:format)     refinery/passwords#edit
                                     PATCH  /refinery/users/password(.:format)          refinery/passwords#update
                                     PUT    /refinery/users/password(.:format)          refinery/passwords#update
                         admin_users GET    /refinery/users(.:format)                   refinery/admin/users#index
                                     POST   /refinery/users(.:format)                   refinery/admin/users#create
                      new_admin_user GET    /refinery/users/new(.:format)               refinery/admin/users#new
                     edit_admin_user GET    /refinery/users/:id/edit(.:format)          refinery/admin/users#edit
                          admin_user PATCH  /refinery/users/:id(.:format)               refinery/admin/users#update
                                     PUT    /refinery/users/:id(.:format)               refinery/admin/users#update
                                     DELETE /refinery/users/:id(.:format)               refinery/admin/users#destroy
                             message GET    /refinery/message(.:format)                 refinery/fast#message
                          admin_root GET    /refinery(.:format)                         refinery/admin/core#index
                       admin_dialogs GET    /refinery/dialogs(.:format)                 refinery/admin/dialogs#index
                        admin_dialog GET    /refinery/dialogs/:id(.:format)             refinery/admin/dialogs#show
      refinery_update_menu_positions POST   /refinery/update_menu_positions(.:format)   refinery/admin/core#update_plugin_positions
                                     GET    /sitemap.xml(.:format)                      refinery/sitemap#index {:format=>"xml"}
                                     GET    /refinery/*path(.:format)                   refinery/admin#error_404
                 marketable_page GET    /*path(.:format)                            refinery/pages#show

来源:https://stackoverflow.com/questions/29155523/rails-4-link-to-url-causes-browser-to-get-when-it-should-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!