设计 - 还记得我不工作吗?本地主机问题?

发布于 2024-11-07 03:04:29 字数 1762 浏览 2 评论 0原文

我正在将 devise 与 Rails 3 应用程序一起使用。由于某种原因,“记住我”登录无法正常工作。

这可能是由于在 localhost:3000 上进行测试造成的吗?

在 devise.rb 中,我有以下设置:

config.remember_for = 2.weeks

在日志中,当我发布登录时,我看到:

Started POST "/users/sign_in" for 127.0.0.1 at Thu May 12 20:53:04 -0700 2011
  Processing by SessionsController#create as HTML
  Parameters: {"signIn"=>"LOG IN", "authenticity_token"=>"GR09TIq4uSbu6UWxDRhpfQeLWp7qtJTxkCFksLmFzdE=", "utf8"=>"✓", "user"=>{"remember_me"=>"on", "password"=>"[FILTERED]", "email"=>"[email protected]"}}

有什么问题吗?

我的session_controller.rb 中也有以下内容 您有

class SessionsController < Devise::SessionsController

  prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
  include Devise::Controllers::InternalHelpers

  # GET /resource/sign_in
  def new
    clean_up_passwords(build_resource)
    render_with_scope :new
  end

  # POST /resource/sign_in
  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "new")
    #set_flash_message :notice, :signed_in
    sign_in_and_redirect(resource_name, resource)
  end

  # GET /resource/sign_out
  def destroy
    #set_flash_message :notice, :signed_out if signed_in?(resource_name)
    sign_out_and_redirect(resource_name)
  end

  protected

  def after_sign_in_path_for(resource)
    if resource.is_a?(User) && resource.banned?
      sign_out resource
      flash[:error] = "This account has been suspended."
      root_path
    else
      super
    end
  end


end

什么想法为什么登录和记住不起作用?谢谢

I'm using devise with my rails 3 app. For some reason the sign in with Remember Me is not working.

Could this be due to testing on localhost:3000 ?

in devise.rb, I have the following set:

config.remember_for = 2.weeks

In the logs, when I post a signin I see:

Started POST "/users/sign_in" for 127.0.0.1 at Thu May 12 20:53:04 -0700 2011
  Processing by SessionsController#create as HTML
  Parameters: {"signIn"=>"LOG IN", "authenticity_token"=>"GR09TIq4uSbu6UWxDRhpfQeLWp7qtJTxkCFksLmFzdE=", "utf8"=>"✓", "user"=>{"remember_me"=>"on", "password"=>"[FILTERED]", "email"=>"[email protected]"}}

Is there anything wrong there?

I also have the following in my sessions_controller.rb

class SessionsController < Devise::SessionsController

  prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
  include Devise::Controllers::InternalHelpers

  # GET /resource/sign_in
  def new
    clean_up_passwords(build_resource)
    render_with_scope :new
  end

  # POST /resource/sign_in
  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "new")
    #set_flash_message :notice, :signed_in
    sign_in_and_redirect(resource_name, resource)
  end

  # GET /resource/sign_out
  def destroy
    #set_flash_message :notice, :signed_out if signed_in?(resource_name)
    sign_out_and_redirect(resource_name)
  end

  protected

  def after_sign_in_path_for(resource)
    if resource.is_a?(User) && resource.banned?
      sign_out resource
      flash[:error] = "This account has been suspended."
      root_path
    else
      super
    end
  end


end

Any ideas why signing in and remembering is not working? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

So尛奶瓶 2024-11-14 03:04:29

发生这种情况是因为 Remember_me 的参数为“on”,但与 Devise::TRUE_VALUES 进行比较,后者为 [true, 1, '1', 't', 'T', 'true', 'TRUE']。

最简单的方法是确保您的 Remember_me 成为该值之一。复选框示例(注意值=“1”):

<input type="checkbox" name="user[remember_me]" value="1" checked="checked" />

如果您想让它与“on”值一起工作,您可以将“on”添加到Devise::TRUE_VALUES。
因此,在您的 config/initializers/devise.rb 中,只需添加第一行:

Devise::TRUE_VALUES << ["on"]

This happens because remember_me comes in params as "on", but is compared to Devise::TRUE_VALUES, which are [true, 1, '1', 't', 'T', 'true', 'TRUE'].

The easiest way is to make it work is to insure your remember_me comes as one of that values. Example of check-box(notice value="1"):

<input type="checkbox" name="user[remember_me]" value="1" checked="checked" />

Another way if you want to make it work with "on" value you can add "on" to Devise::TRUE_VALUES.
So in your config/initializers/devise.rb just add as the first line:

Devise::TRUE_VALUES << ["on"]
呆° 2024-11-14 03:04:29

Devise remember_user_token cookie 可以设置为“仅安全”,在这种情况下,它不能与 http 上的开发 Rails 服务器一起使用(浏览器永远不会将其发送回服务器)。

检查initializers/devise.rb中的rememberable_options = {:secure =>;真的}

The Devise remember_user_token cookie could be set to 'secure only', in which case it doesn't work with the development rails server on http (browser never sends it back to the server).

Check initializers/devise.rb for rememberable_options = {:secure => true}

仅一夜美梦 2024-11-14 03:04:29

您是否还使用 config.timeout_in = 10.分钟 设置了会话?

如果是这样,请参阅 stackoverflow 上的此贡献,它解决了它解决方案

Do you have the sessions set aswell with config.timeout_in = 10.minutes?

If so see this contribution on stackoverflow which solves it solution

美煞众生 2024-11-14 03:04:29

我的问题是 User.rb 中的这一行(我从 Michael Hartl 登录机制更新到设计)

before_save :create_remember_token

我将其注释掉并且它起作用了。

我还有:

User.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,:token_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable

在 devise.rb 上,我只添加了 Devise::TRUE_VALUES << ["on"] 并取消注释 config.remember_for = 2.weeks

My problem with this was this single line in User.rb (I updated from Michael Hartl login mechanism to devise)

before_save :create_remember_token

I commented it out and it worked.

I also have :

User.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,:token_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable

On devise.rb, I only added Devise::TRUE_VALUES << ["on"] and uncommented config.remember_for = 2.weeks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文