在通过自定义初始化程序编写的配置目录中的自定义 yml.erb 文件中使用 image_path 帮助程序

发布于 2024-12-20 17:45:49 字数 1327 浏览 3 评论 0原文

我构建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赤濁 2024-12-27 17:45:49

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

Controller

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

View

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

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

If the ERB only needs to be parsed from inside views, you can do something like this:

Controller

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

View

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

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).

怎樣才叫好 2024-12-27 17:45:49

Rails.application.routes.url_helpers 是一个包含 url_helpers 的模块,您可以将其包含在要使用它们的位置。您可以通过 绑定 将其传递给 ERB

class Application < Rails::Application
  def desktop_icons
    @icons ||= YAML.load(
      ERB.new(File.read("#{Rails.root}/config/icons.yml.erb")).result(binding)
    )
  end
end

在 yml 中

<% extend routes.url_helpers %>
- 
 image: <%= image_path "rails" %>
 title: Test this title

,然后 传递在 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 binding

class Application < Rails::Application
  def desktop_icons
    @icons ||= YAML.load(
      ERB.new(File.read("#{Rails.root}/config/icons.yml.erb")).result(binding)
    )
  end
end

and then in yml

<% extend routes.url_helpers %>
- 
 image: <%= image_path "rails" %>
 title: Test this title

since at erb evaluation time the self is Rails.application

爱你不解释 2024-12-27 17:45:49

正如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文