DEV Community

Jason Crockett
Jason Crockett

Posted on

Solve Sinatra helpers not found, one reason

This code is wrong because access_db is declared outside of a Sinatra::Base block. app in self.registered is a Sinatra base class with functions. helpers cannot exist outside of those functions.

 # require_relative '../helpers/models'

        def self.registered(app)
          clients_db = access_db(Archcare::Logbook::Model::Client)
          clients_for_privilege_level = is_admin? ? clients_db.get_all_documents : get_clients_for_user(current_user_id)
          app.get '/client-report' do
            if logged_in?
              erb :creport,locals: {:title => "New Client Report" ,
                                    :cookie => has_agreed?,
                                    # get all clients from the database
                                    :clients => clients_for_privilege_level}
            elsif !logged_in?
              redirect '/login'
            end
            end

this code produced this error.

C:\Ruby26-x64\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Users/Jason/source/ArchcareLogbook/app.rb
Models is loaded
C:/Users/Jason/source/ArchcareLogbook/routes/client_report.rb:18:in `registered': undefined method `access_db' for Archcare::Logbook::Routes::ClientReport:Module (NoMethodError)
    from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/sinatra-2.0.7/lib/sinatra/base.rb:1415:in `block in register'
    from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/sinatra-2.0.7/lib/sinatra/base.rb:1413:in `each'
    from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/sinatra-2.0.7/lib/sinatra/base.rb:1413:in `register'
    from C:/Users/Jason/source/ArchcareLogbook/app.rb:50:in `<class:Logbook>'
    from C:/Users/Jason/source/ArchcareLogbook/app.rb:30:in `<module:Logbook>'
    from C:/Users/Jason/source/ArchcareLogbook/app.rb:4:in `<module:Archcare>'
    from C:/Users/Jason/source/ArchcareLogbook/app.rb:3:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

Process finished with exit code 1

hope this helps!

Top comments (1)

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

πŸ‘ great post, Add rb to your code block for syntax highlighting.

''' rb

Where single quote ' is a backtick `