使用设备和轨道时,以每个用户的身份设置区域设置3 [英] Setting locale on a per-user basis when using devise and rails 3

查看:59
本文介绍了使用设备和轨道时,以每个用户的身份设置区域设置3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚通过我的一个应用程序将验证从authlogic转换为devise。总的来说,这是非常惊人的直接做,但有一个问题我找不到一个简单的解决方案。



在应用程序中,用户有选择其地区的选项。然后,当他们登录时,他们会以他们选择的语言查看应用程序。以前,我通过简单地在我的UserSessions控制器的create方法中设置区域设置。



使用Devise,所有控制器都将自动设置,这是非常好的。我知道我可以创建一个扩展DeviseController的自定义控制器,并按照这样做,但是从我的理解中,这意味着我也需要创建所有的视图,这似乎有点超过顶部只需要运行一个额外的代码行。



有没有更简单的方法来指定一些代码在成功的设计身份验证上运行?

解决方案

我发现我正在寻找的解决方案 here



正如我只是想为用户设置登录时的区域设置,我所需要的是将以下方法添加到我的ApplicationController中。

  def after_sign_in_path_for resource_or_scope)

如果resource_or_scope.is_a?(用户)&& resource_or_scope.locale!= I18n.locale
I18n.locale = resource_or_scope.locale
end

super
end
/ pre>

I have just been through one of my apps converting the authentication from authlogic to devise. By and large this has been amazingly straight-forward to do but there is one issue I can't find an easy fix for.

In the app, the user has the option of choosing their locale. Then whenever they login, they view the app in the language they choose. Previously, I did this by simply setting the locale in the create method of my UserSessions controller.

With Devise, all the controllers are automatically setup, which is great. I know that I could create a custom Controller that extends the DeviseController and do it like this but, from what I understand, that means that I will also need to create all the views to go with it which seems a bit over the top when I just need to run one extra line of code.

Is there an easier way of specifying some code to be run on a successful devise authentication?

解决方案

I found the solution I was looking for here

As I just wanted to set the locale for the user when they logged in, all I needed was to add the following method to my ApplicationController

def after_sign_in_path_for(resource_or_scope)

  if resource_or_scope.is_a?(User) && resource_or_scope.locale !=  I18n.locale
    I18n.locale = resource_or_scope.locale
  end

  super
end

这篇关于使用设备和轨道时,以每个用户的身份设置区域设置3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆