Sinatra :: Warden undefined方法`认证?'为零:NilClass

时间:2017-08-09 17:57:08

标签: ruby web-applications sinatra rack warden

我尝试使用Sinatra_warden模块对我的Sinatra Web应用程序进行身份验证,但当我指向授权网站时,它说:

NoMethodError at / admin undefined方法`authenticated?'为零:NilClass

这是我的app.rb文件:

<div id="container">
  <div class="product-wrapper">
    <div class="product-img-wrapper">
      <img id="product-img0" src="http://via.placeholder.com/300x200" class="product-img" />
    </div>
    <div class="product-title">
      Titel img0
    </div>
  </div>
  <div class="product-wrapper">
    <div class="product-img-wrapper">
      <img id="product-img1" src="http://via.placeholder.com/200x300" class="product-img" />
    </div>
    <div class="product-title">
      Titel img1
    </div>
  </div>
  <div class="product-wrapper">
    <div class="product-img-wrapper">
      <img id="product-img2" src="http://via.placeholder.com/300x300" class="product-img" />
    </div>
    <div class="product-title">
      Titel img2
    </div>
  </div>
  <div class="product-wrapper">
    <div class="product-img-wrapper">
      <img id="product-img3" src="http://via.placeholder.com/400x300" class="product-img" />
    </div>
    <div class="product-title">
      Titel img3
    </div>
  </div>
  <div class="product-wrapper">
    <div class="product-img-wrapper">
      <img id="product-img4" src="http://via.placeholder.com/300x400" class="product-img" />
    </div>
    <div class="product-title">
      Titel img4
    </div>
  </div>
  <div class="product-wrapper">
    <div class="product-img-wrapper">
      <img id="product-img5" src="http://via.placeholder.com/300x300" class="product-img" />
    </div>
    <div class="product-title">
      Titel img5
    </div>
  </div>
  <div class="product-wrapper">
    <div class="product-img-wrapper">
      <img id="product-img6" src="http://via.placeholder.com/300x200" class="product-img" />
    </div>
    <div class="product-title">
      Titel img6
    </div>
  </div>
  <div class="product-wrapper">
    <div class="product-img-wrapper">
      <img id="product-img7" src="http://via.placeholder.com/200x300" class="product-img" />
    </div>
    <div class="product-title">
      Titel img7
    </div>
  </div>
  <div class="product-wrapper">
    <div class="product-img-wrapper">
      <img id="product-img8" src="http://via.placeholder.com/300x300" class="product-img" />
    </div>
    <div class="product-title">
      Titel img8
    </div>
  </div>
</div>
<!-- container -->

这是我的model.rb文件,以防万一:

require 'sinatra'
require 'sinatra_warden'

class App < Sinatra::Base
  register Sinatra::Warden
enable :sessions

  get '/' do
    erb :index
  end

  get '/admin' do
    authorize!('/login')
    erb :admin
  end

  get '/dashboard' do
    authorize!
    erb :dashboard
  end
end

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

nil错误来自&#34; sinatra_warden&#34;宝石,如果你看一下它试图打电话&#34; request.env [&#39; warden&#39;]&#34;,这是零,因为:

您必须通过&#34;使用&#34;

向Sinatra提供Warden Manager Rack中间件
require 'warden'

class App < Sinatra::Base
  register Sinatra::Warden
  enable :sessions

  use Warden::Manager do |manager|

  ....

完成此操作后,您必须提出Warden策略/配置管理器。

以下是一个例子: https://github.com/sklise/sinatra-warden-example#apprb-cont

似乎sinatra-warden保存了一些样板(给你HAML登录路线),但是由你来配置Warden。