我最近将我的应用程序升级到了 Rails 7,现在我的一些测试失败并出现以下错误。
ActionDispatch::Request::Session::DisabledSessionError: Your application has sessions disabled. To write to the session you must first configure a session store
我怀疑这与
devise
有关。
以下是导致问题的代码。
def login_user(user)
post "/api/v2/login", :params => { :username => user.username, :password => user.password }
assert_response :success, response.body
json = JSON.parse(response.body)
{:authentication_token => json["user"]["authentication_token"]}
end
如果我从
config.api_only = true
文件中删除 application.rb
行,那么测试用例会顺利通过,但由于其他问题,我无法真正删除该行。有人能解决这个问题吗?
我发现了一个类似的问题:Rails 7 API only 应用程序需要使用 Devise 进行会话存储
建议将以下内容添加到您的
config/application.rb
:
config.session_store :cookie_store, key: '_interslice_session'
config.middleware.use ActionDispatch::Cookies
config.middleware.use config.session_store, config.session_options
将下面的代码添加到 config/initializers/devise.rb 对我有用。
config.warden do |manager|
manager.scope_defaults :user, store: false
end