用户会话没有维护

时间:2017-04-26 01:12:12

标签: ruby-on-rails ruby session-cookies

用户可以正常登录,但是在切换到另一个页面时,会话结束。正如您在gif中看到的那样,导航栏的更改就像用户已注销,并且current_user不再处于活动状态。在主页上,应该在欢迎之后显示名字,但事实并非如此。

知道为什么会这样吗?欢呼声。

会话控制器

class LoginsessionsController < ApplicationController

def new
end

def show
end

def create
@user = User.find_by(email: params[:loginsessions][:email].downcase)
if @user && @user.authenticate(params[:loginsessions][:password])
  session[:user_id] = @user.id
  sign_in(@user)
  redirect_to @user
 else
  flash[:notice] = "Invalid email or password"
  render 'new'
 end
end

def destroy
 signout
 session[:user_id] = nil
  respond_to do |format|
   format.html { redirect_to "http://localhost:3000/" , notice: 'You have 
   successfully signout!.'}
  end
 end
end

Sessions Helper

module LoginsessionsHelper

 def sign_in(user)
 remember_token = User.new_remember_token
 cookies.permanent[:remember_token] = remember_token
 user.update_attribute(:remember_token, User.encrypt(remember_token))
 self.current_user = @user
 session[:user_id] = @user.id
 end

 def current_user=(user)
  @current_user = @user
 end

def current_user
 remember_token = User.encrypt(cookies[:remember_token])
 @current_user ||= User.find_by(id: session[:user_id])
end

 def signed_in?
 !current_user.nil?
 end

def signout

   session.delete(:user_id)
  @current_user = nil
 end
end

用户控制器

  class UsersController < ApplicationController
  def index
   @user = User.new
  end
  def show
  @user = User.find(params[:id])
  #unless session[:user_id] == @user.id
 #  redirect_to "http://localhost:3000/loginsessions/new"
 #flash[:notice] = "You do not have access and have been logged out"
 #end
  end

 def new
@user = User.new
 end

 def create
@user = User.new(users_params)
if @user.save
  sign_in @user
  redirect_to @user
else
  render 'new'
end
 end

 def destroy
signout
session[:user_id] = nil
respond_to do |format|
  format.html { redirect_to "http://localhost:3000/loginsessions/new" , 
  notice: 'You have successfully signout!.'}
  end
  end

 private
  def users_params
params.require(:user).permit(:firstName, :lastName, :email, :password, 
:remember_token)
  end
  end

Application.html相关代码

   <% if current_user.present? %>
      <li><%= link_to "Sign out", signout, method: "delete" %></li>
      <li class="trail"><a href="/users/new">Settings</a></li>

      <% else %>
      <li><a href="/loginsessions/new">Login</a></li>
      <li class="trail"><a href="/users/new">Signup</a></li>
      <% end %>

指数(主页)

  <h1 class="welcomeMessage">Welcome
  <% if current_user.present? %>
   <%= current_user.firstName%>

 <% end%></h1>

1 个答案:

答案 0 :(得分:0)

我想我发现了这个问题。在LoginsessionsHelper#sign_in中,未定义变量@usercurrent_user=方法存在同样的问题。我认为应该改变如下:

def sign_in(user)
  remember_token = User.new_remember_token
  cookies.permanent[:remember_token] = remember_token
  user.update_attribute(:remember_token, User.encrypt(remember_token))
  self.current_user = user
  session[:user_id] = user.id
end

def current_user=(user)
  @current_user = user
end