tags:

views:

113

answers:

1

Ok, so I'm not having trouble with this per say, but I want to make sure that I'm doing this right because it just seems like a lot of extra work in the long run.

I am working with Sinatra and using HAML templates. I want to include a JavaScript file from my HAML file. My directory structure looks like this:

  • media/
    • js/
      • init.js (file I want to include)
    • css/
  • models/
  • views/
    • layout.haml
  • routes/
  • init.rb
  • bootstrap.rb (included in init.rb)

When I try to include the file (with no extra routes or anything) I get a page cannot be found error. However, if I add this code, then it works fine:

get '/media/js/:name' do
    begin
        send_file('media/js/' + params[:name])
    rescue
        return "There's nothing for you here"
    end
end

I have no problem doing this for serving my media/static files, but I just want to make sure that this is necessary, I would like to avoid this is possible.

EDIT

I included the following code to my bootstrap.rb file, but to no avail:

set :root, File.dirname(__FILE__)
enable :static
+2  A: 

From Sinatra's Website:

Static files are served from the ./public directory. You can specify a different location by setting the :public option:

set :public, File.dirname(__FILE__) + '/static'

You are serving from the media directory.

Adrian
ok, I'm still getting a page cannot be found error when I used the following code `set :public, File.dirname(__FILE__) + '/media'` in my `bootsrap.rb` file
John
assuming you are going to `localhost:4567/media/js/foo.js`: when you set public, that directory is assumed to be the root dir of the server. So, instead of `localhost:4567/media/js/foo.js`, use `localhost:4567/js/foo.js`
BaroqueBobcat
Or instead, create a folder called public and move `media` into that folder.
Konstantin Haase