user131441
user131441

Reputation:

Sinatra can't serve JavaScript files without help? Is this correct?

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:

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
       "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

Upvotes: 1

Views: 4523

Answers (1)

Adrian
Adrian

Reputation: 15171

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.

Upvotes: 9

Related Questions