lukad
lukad

Reputation: 17853

Access Sinatra settings from helper module

I have a class App that inherits from Sinatra::Base and a module AppHelper that I use with helpers AppHelper in App. How do I access settings defined in App from AppHelper?

Here is some example code:

# app.rb
require "sinatra"
require "./helper"

class App < Sinatra::Base

  set :message, "Hello, World!"
  helpers AppHelper

  get "/" do
    helper_method
  end
end

if __FILE__ == $0
  App.run! port: 4567
end

And here is the helper:

# helper.rb
module AppHelper

  def helper_method
    settings.message
  end
end

Unfortunatley this produces

NoMethodError at /
undefined method `message' for App:Class

How can I access the settings from my helper module?

Upvotes: 0

Views: 1008

Answers (2)

lukad
lukad

Reputation: 17853

My real code is a bit different than my example (I thought it wouldn't matter). I define my settings in config.ru so dax's answer does not work for my app. I was able to solve that problem by using Sinatra::Application.settings instead of settings.

Upvotes: 0

dax
dax

Reputation: 10997

try switching the order of your dependencies:

helpers AppHelper
set :message, "Hello, World!"

Upvotes: 1

Related Questions