我是Ruby on Rails World的新手。
我正在尝试为我的应用程序设置i18n。我可以按照本指南进行操作:http://guides.rubyonrails.org/i18n.html
问题:当我点击像这样的链接时
<%= link_to image_tag("flags/en.png"), :locale=>'en'%>
该URL附加了某种SessionID:
http://localhost:3000/en#.VvHSzGErLVM
如何避免此#.VvHSzGErLVM
出现在网址中?
应用程序控制器
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :set_locale
def set_locale
extracted_locale = params[:locale] || extract_locale_from_accept_language_header
I18n.locale = (I18n::available_locales.include? extracted_locale.to_sym) ?
extracted_locale : I18n.default_locale
end
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'] ? request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first : 'pt'
end
def default_url_options(options = {})
{ locale: I18n.locale }.merge options
end
end
路由
Rails.application.routes.draw do
scope '/(:locale)', locale: /#{I18n.available_locales.join("|")}/ do
root 'home#index'
end
end