在通过自定义初始化程序编写的配置目录中的自定义 yml.erb 文件中使用 image_path 帮助程序
我构建了一个 yml.erb 文件,该文件将用于配置我的应用程序的某些部分。 我想用初始化程序预加载它(我不需要它在应用程序运行期间更改),最大的问题是这个 yml 文件包含指向 app/assets/images 目录内图像的链接。我想在 yml.erb 文件中使用 image_path 帮助程序,但我遇到了麻烦(我不知道应该包含什么以及应该在哪里包含它:如果在 yml.erb 文件中或在解析的文件中) yml.erb 文件)。
我现在拥有的
desktop_icons.rb (在配置/初始化程序内)
require 'yaml'
require 'rails'
include ActionView::Helpers::AssetTagHelper
module ManageFedertrekOrg
class Application < Rails::Application
def desktop_icons
@icons ||= YAML.load(ERB.new(File.read("#{Rails.root}/config/icons.yml.erb")).result)
end
end
end
icons.yml.erb (在配置内)
-
image: <%= image_path "rails" %>
title: Test this title
home_controller.rb (在控制器)
class HomeController < ApplicationController
skip_filter :authenticate_user!
def index
@user_is_signed_in = user_signed_in?
respond_to do |format|
format.html { render :layout => false } # index.html.erb
end
end
def icons
result =
{
icons: MyApp::Application.desktop_icons,
success: true,
total: MyApp::Application.desktop_icons.count
}
respond_to do |format|
format.json { render json: result }
end
end
end
有什么建议吗?
I built a yml.erb file that will be used for configuring some parts of my application.
I would like to preload it with an initializer (I don't require it to change during application running), the biggest problem is that this yml file contains link to images that are inside app/assets/images directory. I would like to use the image_path helper inside my yml.erb file but I'm having troubles (I don't know what I should include and where should I include it: if in the yml.erb file or in the file that parses the yml.erb file).
What I have at the moment
desktop_icons.rb (inside config/initializers)
require 'yaml'
require 'rails'
include ActionView::Helpers::AssetTagHelper
module ManageFedertrekOrg
class Application < Rails::Application
def desktop_icons
@icons ||= YAML.load(ERB.new(File.read("#{Rails.root}/config/icons.yml.erb")).result)
end
end
end
icons.yml.erb (inside config)
-
image: <%= image_path "rails" %>
title: Test this title
home_controller.rb (inside controllers)
class HomeController < ApplicationController
skip_filter :authenticate_user!
def index
@user_is_signed_in = user_signed_in?
respond_to do |format|
format.html { render :layout => false } # index.html.erb
end
end
def icons
result =
{
icons: MyApp::Application.desktop_icons,
success: true,
total: MyApp::Application.desktop_icons.count
}
respond_to do |format|
format.json { render json: result }
end
end
end
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 ERB 只需要从内部视图进行解析,您可以执行以下操作:
Controller
View
这样您就可以控制实际解析哪些属性。此外,由于
(绑定)
,视图中可用的所有帮助程序都可以在 yaml 中使用。If the ERB only needs to be parsed from inside views, you can do something like this:
Controller
View
That way you can control which attributes actually get parsed. Also, all helpers available in the view are available in the yaml, due to
(binding)
.Rails.application.routes.url_helpers
是一个包含 url_helpers 的模块,您可以将其包含在要使用它们的位置。您可以通过 绑定 将其传递给 ERB在 yml 中
,然后 传递在 erb 评估时,自身是
Rails.application
Rails.application.routes.url_helpers
is a module with your url_helpers that you can include where you want to use them. You can pass this to ERB via bindingand then in yml
since at erb evaluation time the self is
Rails.application
正如 ffoeg 和 clyfe 所说,rails 似乎“初始化得不够”。我将脚本移至代码的另一部分,其中 Rails 进行了更多初始化,现在运行良好。
Looks like rails is "not initialized enough" as ffoeg and clyfe stated. I moved the script to another part of my code where rails is more initialized and now it's working well.