ActionController::Base.helpers.image_url 不在初始化程序中生成散列版本
ActionController::Base.helpers.image_url doesnt generate hashed version in initializer
我在 config/initializers 中得到了一个 constants.rb 文件,其内容如下:
DEFAULT_IMAGES = {
profile: ActionController::Base.helpers.image_url('v2/default_profilepic.jpg'),
banner: ActionController::Base.helpers.image_url('v2/profile-banner.jpg'),
missing: ActionController::Base.helpers.image_url('v2/missing.png'),
}
当我尝试在代码中或什至在控制台中的某处调用 DEFAULT_IMAGES 时,我得到了没有哈希的图像。这是它应该做的吗?我的期望是错误的吗?
DEFAULT_IMAGES
=> {:profile=>"http://localhost:3000/assets/v2/default_profilepic.jpg", :banner=>"http://localhost:3000/assets/v2/profile-banner.jpg", :missing=>"http://localhost:3000/assets/v2/missing.png"}
我希望是这样的“/assets/v2/missing-d38d4bdbf9f2cf313e346a844de298c0.png”
config.asset_host = 'http://localhost:3000'
将此行添加到 application.rb 并签入 rails 控制台。
这是解决方法
DEFAULT_IMAGES = {
profile: ActionController::Base.helpers.image_path('v2/default_profilepic.jpg'),
banner: ActionController::Base.helpers.image_path('v2/profile-banner.jpg'),
missing: ActionController::Base.helpers.image_path('v2/missing.png'),
}
您需要使用 *_path
,而不是您预期数据中的 *_url
。
image_path
:计算图像 asset.
的路径
将您的代码放入特定环境文件或 application.rb:
中的 after_initialize 块中
config.after_initialize do
DEFAULT_IMAGES = {
profile: ActionController::Base.helpers.image_url('v2/default_profilepic.jpg'),
banner: ActionController::Base.helpers.image_url('v2/profile-banner.jpg'),
missing: ActionController::Base.helpers.image_url('v2/missing.png'),
}
end
缺点是您需要通过应用程序的命名空间引用您的常量:
Foo::Application::DEFAULT_IMAGES
你也可以试试Proc
proc { ActionController::Base.helpers.image_path('v2/missing.png') }.call
我在 config/initializers 中得到了一个 constants.rb 文件,其内容如下:
DEFAULT_IMAGES = {
profile: ActionController::Base.helpers.image_url('v2/default_profilepic.jpg'),
banner: ActionController::Base.helpers.image_url('v2/profile-banner.jpg'),
missing: ActionController::Base.helpers.image_url('v2/missing.png'),
}
当我尝试在代码中或什至在控制台中的某处调用 DEFAULT_IMAGES 时,我得到了没有哈希的图像。这是它应该做的吗?我的期望是错误的吗?
DEFAULT_IMAGES => {:profile=>"http://localhost:3000/assets/v2/default_profilepic.jpg", :banner=>"http://localhost:3000/assets/v2/profile-banner.jpg", :missing=>"http://localhost:3000/assets/v2/missing.png"}
我希望是这样的“/assets/v2/missing-d38d4bdbf9f2cf313e346a844de298c0.png”
config.asset_host = 'http://localhost:3000'
将此行添加到 application.rb 并签入 rails 控制台。
这是解决方法
DEFAULT_IMAGES = {
profile: ActionController::Base.helpers.image_path('v2/default_profilepic.jpg'),
banner: ActionController::Base.helpers.image_path('v2/profile-banner.jpg'),
missing: ActionController::Base.helpers.image_path('v2/missing.png'),
}
您需要使用 *_path
,而不是您预期数据中的 *_url
。
image_path
:计算图像 asset.
将您的代码放入特定环境文件或 application.rb:
中的 after_initialize 块中config.after_initialize do
DEFAULT_IMAGES = {
profile: ActionController::Base.helpers.image_url('v2/default_profilepic.jpg'),
banner: ActionController::Base.helpers.image_url('v2/profile-banner.jpg'),
missing: ActionController::Base.helpers.image_url('v2/missing.png'),
}
end
缺点是您需要通过应用程序的命名空间引用您的常量:
Foo::Application::DEFAULT_IMAGES
你也可以试试Proc
proc { ActionController::Base.helpers.image_path('v2/missing.png') }.call