【问题标题】:template.path_without_format_and_extension Not working in rails 3.0.3. Getting undefined method templatetemplate.path_without_format_and_extension 在 Rails 3.0.3 中不起作用。获取未定义的方法模板
【发布时间】:2017-11-02 11:46:05
【问题描述】:

我在 rails 3.0.3 中使用 i18n,并且用户具有编辑文本的功能。如果用户保存文本,那么它来自数据库而不是 en.yml 文件。我已经覆盖了 t 助手,如下所示:

def c_t(key, options = {})
    c_key = (scope_key_by_partial_custom(key)).gsub(".","_")
    if $LAYOUT_CONTENTS[c_key].present?
      $LAYOUT_CONTENTS[c_key]
    else
      t(key,options)
    end
  end

然后

def scope_key_by_partial_custom(key)
  if key.to_s.first == "."
    template.path_without_format_and_extension.gsub(%r{/_?}, ".") + key.to_s
  else
    key
  end
end

我正在从视图中调用此方法:

<h1 <%= c_t '.title' %></h1>

它在 rails 2.3 中运行良好,但在将 rails 升级到 3.0.3 后出现错误 **类的未定义方法“模板”

【问题讨论】:

    标签: ruby-on-rails-3 internationalization translation rails-i18n ruby-1.9.3


    【解决方案1】:

    在 rails 3 中使用这个辅助方法

     view_context.instance_variable_get(:@_virtual_path)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2013-04-28
      • 2015-01-22
      相关资源
      最近更新 更多