开发者

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

开发者 https://www.devze.com 2023-01-09 15:09 出处:网络
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.

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]开发者_StackOverflow)
    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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消