简体   繁体   中英

Devise register and login rails

By default with devise. After a user registers it automatically signs them in after sign up. Is there anyway to register a user and not auto sign them in and redirect to the login page?

You need to override Devise::RegistrationsController#create . You can see how registration works here

First, create a custom controller subclass of Devise::RegistrationsController

class RegistrationController < Devise::RegistrationsController
  def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        respond_with resource, location: root_path # Change this to whatever route your want
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
    end
  end 
end

Then tell devise to use custom controller:

devise_for :users, :controllers => {:registrations => "registrations"}

Assuming User is the model of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM