如何在没有登录的情况下注册?

时间:2011-06-02 14:21:45

标签: ruby-on-rails ruby authentication plugins devise

我的应用程序中有一个管理员用户,只有管理员可以在此应用程序中创建和激活用户。

当我创建用户时,设计为这个新用户自动登录。如何在没有自动登录的情况下创建用户?

3 个答案:

答案 0 :(得分:2)

您必须覆盖注册控制器(请参阅this one等教程)

然后,查看原始代码(可以找到here),您必须编辑create部分。

原创

  # POST /resource
  def create
    build_resource

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_in(resource_name, resource)
        respond_with resource, :location => redirect_location(resource_name, resource)
      else
        set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format?
        expire_session_data_after_sign_in!
        respond_with resource, :location => after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords(resource)
      respond_with_navigational(resource) { render_with_scope :new }
    end
  end

您要找的是正在删除此行sign_in(resource_name, resource)

我希望我能正确理解你的问题。

答案 1 :(得分:0)

我不太确定你想要达到的目标:

  1. 只需创建一个用户实例,然后即可登录

  2. 创建新用户并通知他们已创建帐户(即“邀请他们”)

  3. 在第一种情况下,只需创建一个包含相应信息的User实例(检查您需要在控制台中填写哪些字段:它们取决于您的配置和您使用的“策略”:可确认,可锁定,等)

    在第二种情况下,您可能希望查看以下内容:https://github.com/scambra/devise_invitable

答案 2 :(得分:0)

假设User是您的模型,请在is_active表中添加一个名为users的布尔字段。然后在active?模型中使用方法User

class User < ActiveRecord::Base

  #this method will be used by devise to determine if the user is "active"
  def active? 
    #Allow user to log in if the user is confirmed AND if we are allowing 
    #the user to login
    super and (not self.confirmed_at.nil?) and self.is_active?
  end
end

要禁止用户登录,请在is_active模型的before_create过滤器中将字段User设置为false。或者,在迁移中将默认值设置为false

is_active设置为true以允许用户登录。