我正在使用Ruby on Rails v3.2.2和globalize3 v0.2.0 ruby-gems。如何改善设置和获取代码块周围的临时变量?
# Note how I set locale variables.
def update
temp_locale = I18n.locale
I18n.locale = 'it' # 'it' stands for italian
@article = Article.find(params[:id])
respond_to do |format|
if @article.update_attributes(params[:article])
format.html { redirect_to article_path(@article), notice: 'Article was successfully updated.' }
else
format.html { render action: "edit" }
end
end
I18n.locale = temp_locale
end
上面的代码按预期工作(也就是它:因为我想为对象更新转换数据我使用下面的代码在我的控制文件(可以这么用globalize3存储正确的数据)更新意大利语言数据库中的翻译数据),但我认为我设置局部变量的方式是错误的,或者至少不是正确的,或者可能是不可维护/可读的。 有一种方法可以改善?或者,一般来说,有一种方法可以改善设置并在代码块周围获取临时变量?你有什么建议?
你可以用一个'around_filter',但矫枉过正一个小的使用。 – Zabba 2012-04-13 22:52:46