Rails 5 API覆盖设计SessionsController注销错误

时间:2016-05-18 07:43:17

标签: devise rails-routing rails-api

我创建了一个rails 5 api项目 并使用

Devise gem 4.1.0
Ruby 2.3.0
Rails 5.0.0.rc1

我运行命令来覆盖SessionsController

rails generate devise:controllers users

我的自定义会话控件是

class Users::SessionsController < Devise::SessionsController
# before_action :configure_sign_in_params, only: [:create]

  # GET /resource/sign_in
  # def new
  #   super
  # end

  # POST /resource/sign_in
  def create
    super
  end

  # DELETE /resource/sign_out
  def destroy
    super
  end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_in_params
  #   devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
  # end
end

我在登录sign_out时遇到错误

127.0.0.1:3000/users/sign_out

错误是

"status": 500,
  "error": "Internal Server Error",
  "exception": "#<NameError: undefined local variable or method `flash' for #<Users::SessionsController:0x00000003d2e8f0>>",
  "traces": {
    "Application Trace": [],
    "Framework Trace": [
      {
        "id": 0,
        "trace": "devise (4.1.1) app/controllers/devise_controller.rb:157:in `set_flash_message'"
      },
      {
        "id": 1,
        "trace": "devise (4.1.1) app/controllers/devise_controller.rb:164:in `set_flash_message!'"
      },
      {
        "id": 2,
        "trace": "devise (4.1.1) app/controller

s / devise / sessions_controller.rb:61:在`verify_signed_out_user&#39;&#34;       },

我在routes.rb中的当前路线

devise_for :users,
             controllers: {
                sessions: 'users/sessions',
                registrations: 'users/registrations'
             }

我调试但路径没有路由到app / controller / users / SessionsController 我该如何路由该路径

1 个答案:

答案 0 :(得分:1)

需要更改销毁才能在获取请求中执行

进入config / enitializers / devise.rb并在第239行更改

config.sign_out_via = :delete

config.sign_out_via = :get