Rails rails 国际化取 yml 值时发现的问题求解答。

542307299 · August 15, 2013 · Last by 542307299 replied at August 16, 2013 · 2547 hits

rails 为什么在 model 里能用 I18n.t(),用 t 却报错;而在控制器里却都能用,在 erb 文件里 I18n.t()和 t()也都能用

t 是个 helper

#1 楼 @Rei 我记得好像从一本书上看过说 helper 的方法不是只能页面调用吗,那为什么控制器里也能用 t 呢?

#2 楼 @542307299 只要 include,哪里都能用

helper 本质上是一个 module,只要 include 就可以用。 Controller 和 View 的父类中已经 include 过 I18n 了,所以可以直接用 t(),但是 model 通常用不到 I18n,所以父类中没有默认 include,自然就不行了。

You need to Sign in before reply, if you don't have an account, please Sign up first.