1

GITHUB: https://github.com/coasterb/foo_bar_stackoverflow

I cannot access the style sheet in my public directory with my '/foo/bar' route.

I thought it was an issue where I didn't define the public_folder, but it isn't.

require 'sinatra'
require 'haml'
set :static, true
set :public_folder, "public"  

get '/foo' do 
    haml :foo
end

get '/foo/bar' do
    haml :bar
end

# Directory hierarchy - this doesnt work

+Webapp
--app.rb
-+public
---stylesheet.css 
-+views
---foo.haml
---bar.haml

# Directory hierarchy - This does work but i now have a copy of my files.

+Webapp
--app.rb
-+public
---stylesheet.css 
--+foo
----stylesheet.css
-+views
---foo.haml
---bar.haml

localhost:9396/foo/bar:

#foo/bar.html from the web browser. 
<html> 
    <head>
        <link href='./stylesheet.css' rel='stylesheet' type='text/css'>
    </head> 
<html> 

throws an error in Chrome's console, of "404 not found", but does not throw an error in /foo console.

haml bar.haml:

!!!
%html 
  %head
    %link{:href=>"./stylesheet.css", :rel => "stylesheet", :type => "text/css"}
user2446058
  • 146
  • 10

2 Answers2

1

Leave the leading ./ off; this is going to point to /foo/bar/stylesheet.css as @Arman mentioned in the comments above.

Also, you don't need the :public_folder setting that you have used since you are just setting it to the default value.

%link{:href => '/stylesheet.css', :rel => 'stylesheet', :type => 'text/css'}

If you want to manually set a directory for css files specifically or for example sub-dir it under public you can use

:css_dir
bigtunacan
  • 4,873
  • 8
  • 40
  • 73
  • I removed the :public_folder still no dice. Although i did add another directory in "public" named "foo" and put a copy of stylesheet.css in the foo directory. That worked. But now I have a copy of the stylesheet.css. Thats not good. I also put up a repo. thank you for your input. – user2446058 Oct 03 '14 at 22:01
  • Sorry; I was off about not needing the `/`. I've updated and put a fork out at https://github.com/bigtunacan/foo_bar_stackoverflow where you can take a look as well. – bigtunacan Oct 03 '14 at 23:50
1

Use a layout for the head section:

layout.haml

!!!
%html
  %head
    %link{:href=>"/stylesheet.css", :rel => "stylesheet", :type => "text/css"}
  %body 
    = yield

foo.haml or any other view

%p Hello World

instead of putting everything inside each view like this:

bad foo

!!!
%html
  %head
    %link{:href=>"/stylesheet.css", :rel => "stylesheet", :type => "text/css"}
  %body 
    %p Hello World

Also, Sinatra is at version 1.4.5 now, unless you have some pressing need to stick to 1.3, don't. The same goes for using Ruby v1.9, which is more understandable, but there really aren't many good reasons to use it when v2 is available and will run all 1.9 code fine.

ian
  • 12,003
  • 9
  • 51
  • 107