Ruby on rails 如何在用户创建帐户后自动登录?

Ruby on rails 如何在用户创建帐户后自动登录?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我必须覆盖create方法的registrations控制器,因此现在当用户注册时,它不再在创建帐户后自动登录,但我希望保持这种行为 当前我的创建方法如下所示: def create @user = User.new @user.attributes = params[:user] if cookies[:invcode] @user.inv_code = cookies[:invcode] cookies.delete :i

我必须覆盖create方法的registrations控制器,因此现在当用户注册时,它不再在创建帐户后自动登录,但我希望保持这种行为

当前我的创建方法如下所示:

   def create
     @user = User.new
     @user.attributes = params[:user]

     if cookies[:invcode]
        @user.inv_code = cookies[:invcode]
        cookies.delete :invcode
     end

     if @user.save
        redirect_to :root
     end
  end

如果您使用了诸如sign_in@user或designe的sign_in_and_redirect之类的方法,您可以在create方法的@user.save块中使用该方法

您正在使用Desive吗?如果是,

if @user.save
  sign_in_and_redirect @user, event: :authentication
end
或者,更好的办法是,从“设计”中复制一些代码


您的登录代码是什么?您只需将其放入if@user.save代码块;
  # POST /resource
  def create
    build_resource(sign_up_params)

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" 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 resource
    end
  end