GITHUB:https://github.com/coasterb/foo_bar_stackoverflow
我无法使用"/foo/bar"路由访问公共目录中的样式表。
我认为这是一个我没有定义public_folder
的问题,但事实并非如此。
require 'sinatra'
require 'haml'
set :static, true
set :public_folder, "public"
get '/foo' do
haml :foo
end
get '/foo/bar' do
haml :bar
end
#目录层次结构-这不起作用
+Webapp
--app.rb
-+public
---stylesheet.css
-+views
---foo.haml
---bar.haml
#目录层次结构-这确实有效,但我现在有一个文件的副本。
+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>
在Chrome的控制台中抛出一个错误,即"404 not found",但在/foo控制台中没有抛出错误。
haml-bar.haml:
!!!
%html
%head
%link{:href=>"./stylesheet.css", :rel => "stylesheet", :type => "text/css"}
使用头部的布局:
布局.haml
!!!
%html
%head
%link{:href=>"/stylesheet.css", :rel => "stylesheet", :type => "text/css"}
%body
= yield
foo.haml或任何其他视图
%p Hello World
而不是像这样把所有东西都放在每个视图中:
糟糕的foo
!!!
%html
%head
%link{:href=>"/stylesheet.css", :rel => "stylesheet", :type => "text/css"}
%body
%p Hello World
此外,Sinatra现在是1.4.5版本,除非你有迫切需要坚持1.3,否则不要。使用Ruby v1.9也是如此,这更容易理解,但当v2可用并且可以很好地运行所有1.9代码时,确实没有太多好的理由使用它。
关闭前导./
;这将指向上面评论中提到的@Arman的/foo/bar/stylesheet.css
。
此外,您不需要已使用的:public_folder
设置,因为您只是将其设置为默认值。
%link{:href => '/stylesheet.css', :rel => 'stylesheet', :type => 'text/css'}
如果你想手动为css文件设置一个目录,例如在public下的子目录,你可以使用
:css_dir