【问题标题】:How to use a rails variable when accessing a YAML path访问 YAML 路径时如何使用 rails 变量
【发布时间】:2016-01-23 00:03:52
【问题描述】:

我正在尝试创建一个通用方法,该方法采用我的 Rails 变量(对应于 YAML 键)并获取 YAML 数据。

settings.yml

features:
  first_feature: true
  second_feature: false

导轨功能

def my_function(feature)
  Settings.features.#{feature} # where #{feature} passes my param
end

使用示例:

my_function(first_feature)
# => true

不确定这是否可能——还没有运气。

【问题讨论】:

  • @Jordan Settings 引用 settings.yml 并且链式方法在 settings.yml 中找到 features

标签: ruby-on-rails yaml


【解决方案1】:

How do I convert a string to a class method? 找到解决方案

使用.send(variable) 有效。所以:

def my_function(feature)
  Settings.features.send(feature)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2021-03-16
    • 2023-04-03
    相关资源
    最近更新 更多