【问题标题】:Sinatra static assets are not found when using rackup使用 rackup 时找不到 Sinatra 静态资产
【发布时间】:2014-09-04 05:29:12
【问题描述】:

我有一个使用模块化样式配置的简单 Sinatra 应用程序。当我按照自述文件中的建议使用rackup -p 4567 启动应用程序时,不会提供公用文件夹中的静态资产。但是当我使用shotgun ./config.ru -p 4567 启动它时,它们就会被送达。为什么会这样?这会在生产中发生吗?

这是我的代码:

# config.ru
require 'rubygems'
require 'bundler'
require 'sinatra'
require 'jammit'

Bundler.require
Jammit.package!


require File.expand_path('./stick.rb')
run Stick

这是应用程序 ruby​​ 文件

require 'sinatra/base'

class Stick < Sinatra::Base
  get '/' do
    haml :index
  end
end

【问题讨论】:

    标签: ruby configuration sinatra


    【解决方案1】:

    看起来这个问题有两个很好的答案(现有的都不适合我)。

    首先,在您的 config.ru 文件中,您可以包含以下内容:

    # Replace the directory names to taste
    use Rack::Static, :urls => ['/stylesheets', '/javascripts'], :root => 'public'
    

    或者,如果您通过 rackup 运行您的应用程序,:static 选项默认设置为 false。您可以通过以下咒语来解决此问题:

    class MyApp < Sinatra::Base
      set :static, true
      # ...
    end
    

    【讨论】:

    • From Sinatra docs: The :static setting is enabled by default when the public directory exists. 第二种方法没有帮助我运行 Thin。
    【解决方案2】:

    我遇到了同样的问题,我就这样解决了。 我在 config.ru 中添加了这一行。

    map "/public" do
      run Rack::Directory.new("./public")
    end
    

    我像这样在我的视图中使用静态文件

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

    【讨论】:

      【解决方案3】:

      不肯定,但您可能需要set :root, Stick.root

      (基于How to deploy a modular Sinatra app to Heroku?

      【讨论】:

        【解决方案4】:

        为了让我在通过 config.ru 启动的新 Sinatra 应用程序上工作,我必须做其他答案中建议的两件事:

        class MyApp < Sinatra::Base
          set :static, true
          set :root, File.dirname(__FILE__)
        end
        

        【讨论】:

          【解决方案5】:

          首先在您的 sinatra 项目中创建一个名为“public”的文件夹,然后添加几个文件夹

          • 样式表
          • javascripts
          • 图片

          将您的 CSS、JS 和/或 JPG、PNG(图像)添加到每个文件夹

          最后正如@sirfilip 所说,将以下几行添加到 config.ru 文件中

          map "/public" do
           run Rack::Directory.new("./public")
          end
          

          如果是通用 Sinatra(无框架默认)

          views/layout.erb

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
           <head>
              ...
              <link rel="stylesheet" href="stylesheets/your_file.css">
              <link rel="icon" type="image/ico" href="images/your_image.ico" />
          </head>
          <body>
          <%= yield %>
          ...
          <script src="javascripts/your_js.js"></script>
          

          views/index.erb

                <div class="margin-bottom-30">
                  <div class="row">
                    <div class="col-md-12">
                      <ul class="nav nav-pills">
                        <li class="active"><a href="#">Home <span class="badge">42</span></a></li>
                        <li>...</li>
                      </ul>          
                    </div>
                  </div>
                </div>  
          

          所有图片、样式表和 javascript 都可用于您在 Sinatra 应用中注册的任何 url,问题已解决!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-06
            • 2019-03-30
            • 2012-10-01
            • 1970-01-01
            • 1970-01-01
            • 2014-06-07
            • 1970-01-01
            • 2013-02-12
            相关资源
            最近更新 更多