gpt4 book ai didi

ruby - 浏览器之间共享的 Sinatra Session 数据

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:38 24 4
gpt4 key购买 nike

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

我已尝试实现 session 以确保访问的每个用户都拥有不同版本的应用程序。但是,目前,如果我使用两个不同的浏览器访问它,我会得到相同的数据。

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

这是我的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 没有太多经验,如果这是我的疏忽,我深表歉意。

非常感谢任何帮助。

最佳答案

所以我很确定你的问题的核心是this line您在整个代码中的多个地方都有。根据 ruby docs

This ensures that only one instance of Klass can be created.

您已明确告诉 ruby​​ 永远只允许每个类/模块存在一个副本。我不认为那是你想要的。

要准确推断您要实现的目标有点困难,但我认为在 Web 应用程序中使用 Singleton 并不是正确的解决方案。一旦您运行应用程序的第二个实例(或 Heroku 的说法中的 dyno),它所施加的假设就会崩溃。

关于ruby - 浏览器之间共享的 Sinatra Session 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com