加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在通过自定义初始化程序编写的config目录中的

发布时间:2020-12-17 02:16:38 所属栏目:百科 来源:网络整理
导读:我构建了一个yml.erb文件,用于配置我的应用程序的某些部分. 我想用初始化程序预加载它(我不要求它在应用程序运行期间更改),最大的问题是这个yml文件包含指向app / assets / images目录中的图像的链接.我想在我的yml.erb文件中使用image_path帮助器,但是我遇
我构建了一个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

有什么建议吗?

解决方法

如果只需要从内部视图中解析ERB,您可以执行以下操作:

调节器

@questions = YAML.load_file("#{Rails.root}/config/faq.yml.erb")

视图

<%= ERB.new(@questions[2]["answer"]).result(binding).html_safe %>

这样您就可以控制实际解析的属性.此外,由于(绑定),视图中可用的所有帮助程序都可以在yaml中使用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读