Michael Kohler
Michael Kohler

Reputation: 753

Sinatra / Ruby: access set variable from Model

I have defined some variables:

class App < Sinatra::Base

  set :public_folder, relative('public')
  set :views, relative('views')

I tried to access them from my Model both with

options.public_folder

and

settings.public_folder

Neither of them seem to exist within the scope of the Model. How can I access them else?

Upvotes: 1

Views: 602

Answers (1)

sashaegorov
sashaegorov

Reputation: 1862

Here is the working example:

class App < Sinatra::Base
…
  configure do
    set :public_folder, File.expand_path(File.join(File.dirname(__FILE__), "public"))
    set :root, File.expand_path(File.dirname(__FILE__))
  end
…
end

And here is an example how to access this settings in views. In my case this is HAML:

%p= settings.public_folder
%p= settings.root

And page shows:

/Users/sashaegorov/Development/ruby/rvs/public
/Users/sashaegorov/Development/ruby/rvs

Which are correct paths in my system.

Note: File.expand_path() works very well, it always helps to get real path to file or folder. __FILE__ is also useful here.

Upvotes: 1

Related Questions