Rails。 :en:Symbol 的未定义方法“扫描”
Rails. Undefined method `scan' for :en:Symbol
我正在尝试在我的 rails 应用程序中设置国际化。使用这篇文章
https://lingohub.com/blog/2013/08/internationalization-for-ruby-i18n-gem/
但是我有错误:
Failure/Error: http_accept_language.scan(/^[a-z]{2}/).first
NoMethodError:
undefined method `scan' for :en:Symbol
我的应用控制器:
def set_locale
I18n.locale = extract_locale_from_accept_language_header
end
private
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
可能是什么问题?
Ruby 字符串会响应 #scan
,但不会响应符号。尝试在 header 值上调用 #to_s
,如下所示:
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].to_s.scan(/^[a-z]{2}/).first
end
我正在尝试在我的 rails 应用程序中设置国际化。使用这篇文章 https://lingohub.com/blog/2013/08/internationalization-for-ruby-i18n-gem/
但是我有错误:
Failure/Error: http_accept_language.scan(/^[a-z]{2}/).first
NoMethodError:
undefined method `scan' for :en:Symbol
我的应用控制器:
def set_locale
I18n.locale = extract_locale_from_accept_language_header
end
private
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
可能是什么问题?
Ruby 字符串会响应 #scan
,但不会响应符号。尝试在 header 值上调用 #to_s
,如下所示:
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].to_s.scan(/^[a-z]{2}/).first
end