I have categoried, which user choose. If user choose ‘Auto’ I need to replace it with result, what gives me my JavaScript function – setting default browser language.
Here is JavaScript example, of JS function – http://fiddle.jshell.net/xCgsb/
And here is my before_save filter in Website model:
before_save :auto_language
def create
website = current_user.websites.new params[:website]
if @website.language == "Auto"
@website.language = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
...
#error
undefined local variable or method `request' for #<Website:0x244e1b0>
Can someone help me with this trouble ? Any help appreciated.
You should not be using Javascript to set up your default locale.
Take a look at this:
http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-client-supplied-information
They use Ruby to detect the locale
If you want to get the full language, you can use a switch case:
The ‘else’ case is the default behavior if none of these conditions has been verified.
The keyword ‘return’ is optional.
Hope this helps!