Rails不会设置会话

时间:2019-01-02 04:44:13

标签: ruby-on-rails ruby

我有一个登录表单,该表单应该在提交正确的凭据后创建一个会话,但是该会话没有保存。是什么原因造成的?

sessions_controller:

class SessionsController < ApplicationController
  def new
  end

  def create
    @user = User.find_by_username(params[:session][:username])
    if @user && @user.authenticate(params[:session][:password])
      session[:id] = @user.id
      redirect_to '/posts'
    else
      session[:id] = nil
      flash[:warning] = "Failed login- try again"
      redirect_to '/login'
    end 
  end

  def destroy
    session[:user_id] = nil 
    redirect_to login_path 
  end
end

迁移:

class CreateSessions < ActiveRecord::Migration[5.2]
  def change
    create_table :sessions do |t|

      t.integer :session_id
      t.timestamps
    end
  end
end

1 个答案:

答案 0 :(得分:0)

我假设您正在使用activerecord-session_store gem在表中存储会话。

将此添加到initializers / session_store.rb

require "action_dispatch/session/active_record_store"

Rails.application.config.session_store :active_record_store, :key => 
 '_lotuspay_session'