我构建了一个 yml.erb 文件,该文件将用于配置我的应用程序的某些部分。我想用初始化程序预加载它(我不需要在应用程序运行期间更改它),最大的问题是这个 yml 文件包含指向 app/assets/images 目录内的图像的链接。我想在我的 yml.erb 文件中使用 image_path 帮助程序,但我遇到了麻烦(我不知道我应该包含什么以及应该在哪里包含它:如果在 yml.erb 文件中或在解析的文件中yml.erb 文件)。
我现在拥有的
desktop_icons.rb(在 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(内部配置)
-
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
有什么建议吗?