【发布时间】:2013-12-24 12:25:11
【问题描述】:
在我的多语言 Rails 应用程序中,我有一个 ProfilesController。我正在使用 GET 变量 section 将视图拆分为不同的选项卡。我还将该变量存储在 session 中,以便在请求中记住它。
class ProfilesController < ApplicationController
before_action :signed_in_user
before_action :find_profile
before_action :set_section, :only => [:show, :edit]
def show
end
def edit
end
def update
if @profile.update_attributes(profile_params)
flash[:success] = t('profiles.flash_messages.profile_updated')
redirect_to edit_profile_path(:section => session[:section])
else
@title = t("views.#{session[:section]}")
render :edit
end
end
private
def find_profile
@profile = current_user.profile
end
def set_section
section = Profile::SECTIONS.include?(params[:section]) ? params[:section] : Profile::SECTIONS[0]
session[:section] = section
end
我不明白为什么我的update 操作会不断重定向到default_locale,而不是用户选择并因此存储在会话中的语言环境。
谁能告诉我这里缺少什么?
这是我的routes.rb 文件的摘录:(请注意,我在这里使用单数资源,因为每个用户只能拥有一个个人资料)
MyApp::Application.routes.draw do
scope '(:locale)' do
resource :membership
...
get 'change_locale', :to => 'locales#change_locale'
end
end
更新:
class ApplicationController < ActionController::Base
before_action :set_locale
protected
def set_locale
if params[:locale]
if I18n.available_locales.include?(params[:locale].to_sym)
I18n.locale = session[:locale] || params[:locale] || I18n.default_locale
end
end
end
def default_url_options
{ :locale => I18n.locale }
end
end
【问题讨论】:
-
你能显示区域设置的位置吗?
-
@SergeyKutsko:刚刚更新了我的问题。
-
已更新。别忘了我)
-
您在哪里设置和更改会话[:locale]?更新帖子。
标签: ruby-on-rails ruby ruby-on-rails-3 internationalization rails-i18n