浏览器之间共享的Sinatra会话数据

时间:2015-07-15 21:31:10

标签: ruby session heroku sinatra

我使用Sinatra在Heroku上运行了一个基本的银行应用程序。

我尝试过实施会话,以确保每个访问者都有不同版本的应用。但是,目前,如果我使用两个单独的浏览器访问它,我有相同的数据。

没有实现后端数据库,但我通过界面添加的数据在我访问的每个浏览器中都存在。

这是我的app.rb

require 'sinatra/base'
require 'tilt/erb'
require 'require_all'
require_all 'lib'
require 'rufus-scheduler'

class BankingApp < Sinatra::Base

  enable :sessions
  set :session_secret, 'super secret'

  get '/' do
    session[:accounts] = AccountsController.instance
    session[:holders]  = HoldersController.instance
    session[:loans]    = LoansController.instance 
    erb :index
  end

  get '/holders' do
    @holders = session[:holders].store
    erb :holders
  end

  get '/holders_accounts' do
    @holder = session[:holders].find(params[:id].to_i)
    message = session[:accounts].get_accounts_of(params[:id].to_i)
    @accounts = message.accounts
    erb :holders_accounts
  end

  get '/new_holder' do
    erb :new_holder
  end

  post '/new_holder' do
    @message = session[:holders].create(params[:name])
    @holders = session[:holders].store
    erb :holders
  end

  get '/create_account' do
    erb :create_account
  end

  post '/create_account' do
    type = :Current
    id = params[:id].to_i
    @message = session[:accounts].open(type, with: id)
    erb :index
  end

  get '/accounts' do
    @accounts = session[:accounts].store
    erb :accounts
  end

  get '/transactions' do
    message = session[:accounts].get_transactions_of(params[:id].to_i)
    @transactions = message.transactions
    erb :transactions
  end

  get '/deposit' do
    erb :deposit
  end

  post '/deposit' do
    @accounts = session[:accounts].store
    @message = session[:accounts].deposit(params[:amount].to_i, into: params[:id].to_i)
    erb :accounts
  end

  get '/withdraw' do
    erb :withdraw
  end

  post '/withdraw' do
    @accounts = session[:accounts].store
    @message = session[:accounts].withdraw(params[:amount].to_i, from: params[:id].to_i)
    erb :accounts
  end

  get '/transfer' do
    erb :transfer
  end

  post '/transfer' do
    @accounts = session[:accounts].store
    @message = session[:accounts].transfer(params[:amount].to_i, from: params[:donar].to_i, to: params[:recipitent].to_i)
    erb :accounts
  end

  get '/add_holder' do
    erb :add_holder
  end

  post '/add_holder' do
    @accounts = session[:accounts].store
    @message = session[:accounts].add_holder(params[:holder_id].to_i, to: params[:account_id].to_i)
    erb :accounts
  end

  get '/enable_overdraft' do
    erb :enable_overdraft
  end

  post '/enable_overdraft' do
    @accounts = session[:accounts].store
    @message = session[:accounts].activate_overdraft(params[:id].to_i, params[:amount].to_i)
    erb :accounts
  end

  get '/disable_overdraft' do
    erb :disable_overdraft
  end

  post '/disable_overdraft' do
    @accounts = session[:accounts].store
    @message = session[:accounts].deactivate_overdraft(params[:id].to_i)
    erb :accounts
  end

  get '/loans' do
    @loans = session[:loans].store
    erb :loans
  end

  get '/loan_view' do
    message = session[:loans].show(params[:id].to_i)
    @transactions = message.transactions
    erb :loan_view
  end

  get '/new_loan' do
    erb :new_loan
  end

  post '/new_loan' do
    @loans = session[:loans].store
    id = params[:id].to_i
    options = { borrowed: params[:amount].to_i, term: params[:term].to_i, rate: params[:rate].to_f }
    @message = session[:loans].create_loan(id, options)
    erb :loans
  end

  get '/pay_loan' do
    erb :pay_loan
  end

  post '/pay_loan' do
    @message = session[:loans].pay(params[:amount].to_i, off: params[:id].to_i)
    @loans = session[:loans].store
    erb :loans
  end

  # start the server if ruby file executed directly
  run! if app_file == $0
end

我对Sinatra没有太多的经验,如果这是我的疏忽,请道歉。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

因此,我非常确定您的问题的核心是this line,您可以在整个代码中的多个位置使用它。根据{{​​3}}

  

这确保只能创建一个Klass实例。

您已明确告知ruby只允许每个类/模块的一个副本存在。我不认为这就是你想要的。

有点难以推断出你是什么并且正在努力实现目标,但我不认为在网络应用中使用Singleton将是正确的解决方案。一旦您运行应用程序的第二个实例(或Heroku用语中的dyno),它所假设的假设就会中断。

答案 1 :(得分:0)

我建议使用此

  

获取'/ logout'做

     

session.clear