也许是一个非常愚蠢的问题,请不要为此标记我,但我终于让 Heroku 使用 asset_sync 在我的 S3 存储桶中编译其静态 Assets 。
现在我怎么知道 Assets 实际上是从那里提供的,我认为没有什么魔法可以将它们从 s3 拉进来?我必须为每个 Assets 设置路径,前缀为
https://s3-eu-west-1.amazonaws.com/pathto/asset
有没有办法在 sinatra 中显式设置它,我不必手动更改每个 Assets ,是吗?那太愚蠢了。
asset_sync 文档说要在 Rails 中使用它
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
但我不知道如何在 sinatra 中设置它
编辑
require 'bundler/setup'
Bundler.require(:default)
require 'active_support/core_ext'
require './config/env' if File.exists?('config/env.rb')
require './config/config'
require "rubygems"
require 'sinatra'
configure :development do
AssetSync.config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
end
get '/' do
erb :index
end
get '/about' do
erb :about
end
这会在控制台中出现以下错误
undefined method `action_controller' for #<AssetSync::Config:0x24d1238> (NoMethodError)
最佳答案
尝试将其放入 Sinatra's configure block通过Async Built-in initializer ,例如:
configure :production do
AssetSync.config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
end
您可能在某个时候也必须调用 AssetSync.sync
,我不确定。
编辑:使用配置 block 。
如果您使用的是模块化应用程序(如果没有,则没有任何不同,只需删除 class
位即可)
class App < Sinatra::Base
configure :development do
set :this, "and that"
enable :something
set :this_only, "gets run in development mode"
end
configure :production do
set :this, "to something else"
disable :something
set :this_only, "gets run in production"
# put your AssetSync stuff in here
end
get "/" do
# …
end
get "/assets" do
# …
end
post "/more-routes" do
# …
end
# etc
end
请参阅我上面添加的链接了解更多信息。
action_controller
是 Rails 的一部分。要为路径添加前缀,最好的办法是使用助手:
helpers do
def aws_asset( path )
File.join settings.asset_host, path
end
end
configure :production do
set :asset_host, "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
end
configure :development do
set :asset_host, "/" # this should serve it from the `public_folder`, add subdirs if you need to.
end
然后在路线或 View 中您可以执行以下操作:
aws_asset "sprite_number_1.jpg"
与 ERB 和 sinatra-static-assets's image_tag 一起使用:
image_tag( aws_asset "sprite_number_1.jpg" )
或将它们组合起来(这可能不起作用,因为 image_tag
帮助器可能不会在帮助器的范围内看到,尝试它比考虑它更容易):
helpers do
def aws_image( path )
image_tag( File.join settings.asset_host, path )
end
end
# in your view
aws_image( "sprite_number_1.jpg" )
我确信会有一种更简单的方法来做到这一点,但这将是一个快速而肮脏的解决方案。
关于ruby - 我的 Assets 是否由 AWS S3 提供服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15510312/