Rails 4 - 设置会话变量,以便用户只能在该会话期间查看页面



我正在使用rails 4,并希望给出包含一些个人信息的即时报价。我需要使报价在某人的整个会话中可见,而无需他们登录,但其他人会话无法看到该报价。这个想法是他们可以看到我给他们的报价,并将该报价下载为PDF。这是我控制器中的本质 - 但它不起作用。

class ClientsController < ApplicationController
  before_filter :check_guest, :only => :show
def check_guest
  # # if user isn't logged in
  if current_user.nil? 
  #   # if user has already viewed, redirect
  #   if session[:viewed] == true
  #     flash[:alert] = "You can only view a quote once. Please resubmit your information."
  #     redirect_to root_path
  #   # if user hasn't viewed, allow access, but flag as having viewed
  #   else
  #     session[:viewed] = true
  #   end
    if session[:quote_id] == params[:id] # where params[:id] is the quote ID
      flash[:alert] = "Allow"
    else
      flash[:alert] = "Dont Allow"
    end        
  end
end

我想我需要设置变量 session[:quote_id] = @client.id 但不确定我是在创建操作还是显示操作中这样做。 无论我把它放在哪个地方,整个代码都不起作用。任何帮助将不胜感激。谢谢。

我需要

做的两件事。

首先,在创建中将会话变量设置为 LAST 操作。

def create
....some actions
session[:quote_id] = @client.id
end

接下来,我必须将参数 ID 转换为整数,以便它能够正确计算。

session[:quote_id] == (params[:id].to_i)

最新更新