每次都使用设计GETing而不是在Rails 4中进行POST

时间:2013-11-03 02:41:49

标签: ruby-on-rails devise ruby-on-rails-4

所以这似乎是一个非常普遍的问题。我已经梳理了github上的问题跟踪器以及Stackoverflow,但仍未找到解决方案 - 将config.sign_out_via = :delete更改为config.sign_out_via = :get - 这显然是非常糟糕的做法。

我的问题是,无论我尝试什么,客户都会发送get请求,而不是规定的deletepost

在我看来
<%= link_to "sign out", destroy_user_session_path, :method => :delete %>

rake路由提供

Anchor$ rake routes
                  Prefix Verb   URI Pattern                                   Controller#Action
                    root GET    /                                             home#index
        new_user_session GET    /login(.:format)                              devise/sessions#new
            user_session POST   /login(.:format)                              devise/sessions#create
    destroy_user_session DELETE /logout(.:format)                             devise/sessions#destroy
           user_password POST   /password(.:format)                           devise/passwords#create
       new_user_password GET    /password/new(.:format)                       devise/passwords#new
      edit_user_password GET    /password/edit(.:format)                      devise/passwords#edit
                         PATCH  /password(.:format)                           devise/passwords#update
                         PUT    /password(.:format)                           devise/passwords#update
cancel_user_registration GET    /cancel(.:format)                             devise/registrations#cancel
       user_registration POST   /                                             devise/registrations#create
   new_user_registration GET    /sign_up(.:format)                            devise/registrations#new
  edit_user_registration GET    /edit(.:format)                               devise/registrations#edit
                         PATCH  /                                             devise/registrations#update
                         PUT    /                                             devise/registrations#update
                         DELETE /                                             devise/registrations#destroy
         user_activities GET    /users/:user_id/activities(.:format)          activities#index
                         POST   /users/:user_id/activities(.:format)          activities#create
       new_user_activity GET    /users/:user_id/activities/new(.:format)      activities#new
      edit_user_activity GET    /users/:user_id/activities/:id/edit(.:format) activities#edit
           user_activity GET    /users/:user_id/activities/:id(.:format)      activities#show
                         PATCH  /users/:user_id/activities/:id(.:format)      activities#update
                         PUT    /users/:user_id/activities/:id(.:format)      activities#update
                         DELETE /users/:user_id/activities/:id(.:format)      activities#destroy
                   users GET    /users(.:format)                              users#index
                         POST   /users(.:format)                              users#create
                new_user GET    /users/new(.:format)                          users#new
               edit_user GET    /users/:id/edit(.:format)                     users#edit
                    user GET    /users/:id(.:format)                          users#show
                         PATCH  /users/:id(.:format)                          users#update
                         PUT    /users/:id(.:format)                          users#update
                         DELETE /users/:id(.:format)                          users#destroy

的routes.rb

root :to => "home#index"
  devise_for :users

  resources :users do
    resources :activities
  end

js manifest

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .

application.html.erb 包含适当的<%= javascript_include_tag "application" %>以包含ujs,呈现的标题包括:

<script src="/assets/jquery.js?body=1">
<script src="/assets/jquery_ujs.js?body=1">

我有一种感觉,它可能与Turbolinks有关,所以我尝试了data-no-turbolink的多种变体,但没有雪茄。无论如何,客户端总是发送Started GET "/users/sign_out"来保持路由到UsersController#show。

任何见解都会有所帮助!

2 个答案:

答案 0 :(得分:0)

在您的视图中尝试此操作

<a data-method="delete" href="<%= destroy_user_session_path %>">Logout</a>

答案 1 :(得分:0)

与我同样的问题:( 现在在Rails 4中,至少我现在所做的就是路由

get '/users/sign_out' => 'devise/sessions#destroy'

*但这种方法容易受到CSRF攻击