【问题标题】:Rails 3 override Devise sessions controllerRails 3 覆盖设计会话控制器
【发布时间】:2011-12-25 14:24:00
【问题描述】:

我需要在登录过程中覆盖 Devise 会话控制器(Rails 3.0.9、Ruby 1.9.2、Devise 1.3.4),我试过这个没有任何效果

class SessionsController < Devise::SessionsController

  # GET /resource/sign_in
  def new
    resource = build_resource
    clean_up_passwords(resource)
    respond_with_navigational(resource, stub_options(resource)){ render_with_scope :new }
  end

end

想法?

编辑 如答案所示,我还需要更改路线。此外,我还需要复制视图。在这里解释得更好 http://presentations.royvandewater.com/authentication-with-devise.html#8

我的自定义策略:

devise.rb
config.warden do |manager|
  manager.strategies.add(:custom_strategy) do
    def authenticate!
      ... authenticate against 3rd party API...
      if res.body =~ /success/
        u = User.find_or_initialize_by_email(params[:user][:email])
        if u.new_record?
          u.save
        end
      success!(u)
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise


    【解决方案1】:

    您是否更改了路线以使用新控制器?

    /config/routes.rb
    
      devise_for :users, :controllers => {:sessions => "sessions"}
    

    【讨论】:

    • 是的,我也想通了,我还需要复制看起来的视图。除了上述更改,我还保留原来的devise_for :users 路由吗?我不想覆盖其他操作。
    • 这个还有一个问题,除了我添加的自定义策略之外,它也没有执行默认的数据库认证策略,不知道如何解决这个问题。
    • 应该只有 1 个devise_for,并且在您复制视图后,即使您未覆盖的操作也应该可以正常工作。至于你的其他问题,我没有完全关注。您的模型中是否设置了:database_authenticatable 选项。 (即设计:database_authenticable)
    • 是的,:database_authenticable 一直在为User 服务。此外,我还有一个自定义策略,有关详细信息,请参阅已编辑的问题。
    • 我的自定义策略仍在执行,但如果无法通过身份验证,它将不再落入:database_authenticable
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2011-04-02
    • 1970-01-01
    相关资源
    最近更新 更多