我想在保存之前检查一些字段并将其更改为默认浏览器语言。
我想使用 before_save 过滤器:
def update
@website = Website.find(params[:id])
if @website.language == "Automatic (by user's browser language)"
@website.language = full_language(request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first)
end
respond_to do |format|
if @website.update_attributes(params[:website])
format.html { redirect_to @website,
notice: 'Note: code has been updated. Please replace the code you have on your website with the code below. Only then changes will take effect.'}
format.js
end
end
end
我需要检查:
if @website.language == "Automatic (by user's browser language)"
@website.language = full_language(request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first)
end
它适用于创建操作,但不适用于更新。
我怎样才能做到这一点 ?