Sinatra无法从帮助文件中设置cookies。

问题描述 投票:1回答:1

我的sinatra应用程序中有一个helper文件,其中有以下代码。

todo_sinatra_apphelperssessions_helper.rb

class SessionsHelper

  def self.sign_in(user)
    cookies[:remember_token] = { value: user.remember_token, expires: 20.years.from_now.utc }
    self.current_user = user
  end

...
end

当我试图从app.rb文件中调用sign_in方法时,它抛出一个错误,说它不知道如何创建cookie。

require 'sinatra'
require 'pg'
require 'sinatra/activerecord'
require 'sinatra/contrib'
require 'sinatra/cookies'
require './helpers/sessions_helper'
...

post '/sign_in' do
  user = User.find_by(email: params[:email])
  if user && user.authenticate(params[:password])
    SessionsHelper.sign_in(user)
    redirect '/'
  else
  ...
end

enter image description here

下面是我的gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    activemodel (5.2.0)
      activesupport (= 5.2.0)
    activerecord (5.2.0)
      activemodel (= 5.2.0)
      activesupport (= 5.2.0)
      arel (>= 9.0)
    activesupport (5.2.0)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
    arel (9.0.0)
    backports (3.16.0)
    bcrypt (3.1.13)
    byebug (11.0.1)
    coderay (1.1.2)
    concurrent-ruby (1.1.5)
    daemons (1.3.1)
    diff-lcs (1.3)
    eventmachine (1.2.7)
    i18n (1.8.2)
      concurrent-ruby (~> 1.0)
    method_source (0.9.2)
    minitest (5.14.0)
    multi_json (1.14.1)
    mustermann (1.1.1)
      ruby2_keywords (~> 0.0.1)
    pg (1.2.2)
    pry (0.12.2)
      coderay (~> 1.1.0)
      method_source (~> 0.9.0)
    pry-byebug (3.7.0)
      byebug (~> 11.0)
      pry (~> 0.10)
    rack (2.1.2)
    rack-protection (2.0.8.1)
      rack
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rake (13.0.1)
    rspec (3.9.0)
      rspec-core (~> 3.9.0)
      rspec-expectations (~> 3.9.0)
      rspec-mocks (~> 3.9.0)
    rspec-core (3.9.1)
      rspec-support (~> 3.9.1)
    rspec-expectations (3.9.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.9.0)
    rspec-mocks (3.9.1)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.9.0)
    rspec-support (3.9.2)
    ruby2_keywords (0.0.2)
    sinatra (2.0.8.1)
      mustermann (~> 1.0)
      rack (~> 2.0)
      rack-protection (= 2.0.8.1)
      tilt (~> 2.0)
    sinatra-activerecord (2.0.14)
      activerecord (>= 3.2)
      sinatra (>= 1.0)
    sinatra-contrib (2.0.8.1)
      backports (>= 2.8.2)
      multi_json
      mustermann (~> 1.0)
      rack-protection (= 2.0.8.1)
      sinatra (= 2.0.8.1)
      tilt (~> 2.0)
    sinatra-flash (0.3.0)
      sinatra (>= 1.0.0)
    thin (1.7.2)
      daemons (~> 1.0, >= 1.0.9)
      eventmachine (~> 1.0, >= 1.0.4)
      rack (>= 1, < 3)
    thread_safe (0.3.6)
    tilt (2.0.10)
    tzinfo (1.2.6)
      thread_safe (~> 0.1)

PLATFORMS
  ruby

DEPENDENCIES
  activerecord (= 5.2)
  bcrypt
  minitest (= 5.14.0)
  pg
  pry-byebug
  rack-test
  rake
  rspec
  sinatra
  sinatra-activerecord
  sinatra-contrib
  sinatra-flash
  thin

BUNDLED WITH
   2.1.4

我试着把require'sinatracookies'放在helper文件里,并确保捆绑了'sinatra-contrib'这个 gem。当我直接在app.rb文件中设置cookie时,它就会工作。有谁能建议我检查一下其他的东西,或者是什么问题?

ruby cookies sinatra
1个回答
3
投票

当你 require 'sinatra' 某些神奇的事情发生了,它把一堆东西带入了范围,并且基本上把你的app.rb变成了一个类似于 Sinatra::Application. 该 cookies 方法只在这样的实例上定义,而不会自动出现在其他类上。

你可能想做的是把你的帮助程序变成一个真正的Sinatra风格的帮助程序,把它做成一个模块,然后使用 helpers 关键字,这将只是使这些实例方法。

module SessionsHelper

  def sign_in(user)
    cookies[:remember_token] = { value: user.remember_token, expires:     20.years.from_now.utc }
    self.current_user = user
  end

...
end

而在你的主文件中,

require './helpers/sessions_helper'
helpers SessionsHelper

...

post '/sign_in' do
  user = User.find_by(email: params[:email])
  if user && user.authenticate(params[:password])
    sign_in user
    redirect '/'
  else
  ...
end

你可能会喜欢 在 README 中阅读更多信息.

© www.soinside.com 2019 - 2024. All rights reserved.