2017-09-30 46 views
3

我正在使用Devise進行身份驗證。我已登錄網站。登錄創建後用戶公司。我如何銷燬當前會話併爲新創建的用戶創建新會話。已經登錄如何覆蓋用戶會話+ devise + rails

請看代碼。

----> sessions_controller.rb

class Users::SessionsController < Devise::SessionsController 
    # before_action :configure_sign_in_params, only: [:create] 

    # GET /resource/sign_in 
    # def new 
    # super 
    # end 

    # POST /resource/sign_in 
    # def create 
    # super 
    # end 

    # DELETE /resource/sign_out 
    # def destroy 
    # super 
    # end 

    # protected 

    # If you have extra params to permit, append them to the sanitizer. 
    # def configure_sign_in_params 
    # devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) 
    # end 
end 

---->的routes.rb

Rails.application.routes.draw do 

    devise_for :users, controllers: { 
      sessions: 'users/sessions' 
     } 
    root to: "companies#index" 

    resources :companies do 
    end 
    resources :company_users do 
    end 

end 

----> companies_controller

def create 
    @company = Company.new(company_params) 
    respond_to do |format| 
     if @company.save 
     format.html { redirect_to @company, notice: 'Company was successfully created.' } 
     format.json { render :show, status: :created, location: @company } 
     else 
     format.html { render :new } 
     format.json { render json: @company.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

我在這裏創建我的用戶和公司。完成後,我想爲這個新創建的用戶創建會話。

任何建議也歡迎以及請給出具體答案。非常感謝。

+0

你可以更詳細說明你想重置會話或模擬用戶。 –

+0

我找到了我的解決方案。正如我寫的,我想關閉當前用戶會話併爲新用戶創建一個新會話。 – wish

回答

3

從我猜測,你的公司有很多用戶。如果你想登錄近期成立的公司的第一個用戶,你可以做一些事情像

reset_session sign_in @company.users.first

創建公司之後。

reset_session是一種rails方法,它將刪除當前會話,sign_in object是一種可用於以任何用戶身份登錄的設計方法。

這應該有所幫助。

相關問題