2016-05-10 2 views
1

В моих контроллерах приложений есть код до, который должен перенаправить каждый тип пользователя на соответствующую страницу после входа. Когда каждый тип пользователя подписывается, они должен быть перенаправлен на корневую страницу, это то же самое с выходом из системы. Когда я нажимаю кнопку входа в систему или кнопку регистрации, я переадресую ее на страницу customer_dashboard вместо входа в систему или страницы регистрации. Когда я удаляю выход, я получаю эту ошибку Cannot redirect to nil!. Перед добавлением этих методов перенаправления, когда я нажимаю на каждую кнопку, они ссылаются на правильные страницы для входа в систему и регистрации. Что не так с этим кодом? Есть ли какая-то логика, что код отсутствует, чтобы этот сайт работал?вход в систему, вход в систему и регистрация не разрешены.

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    before_action :authenticate! 

    def after_sign_in_path_for(resource) 
     case resource 
      when Customer then customer_dashboard_path 
      when Vendor then stored_location_for(:vendors) || vendor_dashboard_path 
     end 
    end 

    def after_sign_out_path_for(resource) 
     case resource 
      when Customer then root_path 
      when Vendor then stored_location_for(:vendors) || root_path 
     end 
    end 

    def after_inactive_sign_up_path_for(resource) 
     case resource 
      when Customer then root_path 
      when Vendor then stored_location_for(:vendors) || root_path 
     end 
    end 

    def authenticate! 
     if @current_user == current_customer 
      :authenticate_customer! 
      elsif @current_user == current_vendor 
      :authenticate_vendor! 
     end 
    end 

end 

поэтому я добавил skip_before_filter: аутентифицировать! для всех 4 контроллеров и проблема маршрутизации все еще сохраняется.

class Vendors::RegistrationsController < Devise::RegistrationsController 
# before_action :configure_sign_up_params, only: [:create] 
# before_action :configure_account_update_params, only: [:update] 
skip_before_filter :authenticate! 
    # GET /resource/sign_up 
    # def new 
    # super 
    # end 

    # POST /resource 
    # def create 
    # super 
    # end 

    # GET /resource/edit 
    # def edit 
    # super 
    # end 

    # PUT /resource 
    # def update 
    # super 
    # end 

    # DELETE /resource 
    # def destroy 
    # super 
    # end 

    # GET /resource/cancel 
    # Forces the session data which is usually expired after sign 
    # in to be expired now. This is useful if the user wants to 
    # cancel oauth signing in/up in the middle of the process, 
    # removing all OAuth session data. 
    # def cancel 
    # super 
    # end 

    # protected 

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

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

    # The path used after sign up. 
    # def after_sign_up_path_for(resource) 
    # super(resource) 
    # end 

    # The path used after sign up for inactive accounts. 
    # def after_inactive_sign_up_path_for(resource) 
    # super(resource) 
    # end 
end 

ответ

1

Это происходит из-за before_action :authenticate!, как она вызывается перед каждым действием и неправильно маршрутизации вас.

Я не думаю, какова ваша цель, но вы должны пропустить это перед фильтром перед некоторыми действиями, такими как ваш логин или регистрация.

+0

Как исправить эту ошибку? жаль, что я очень новичок в развитии rails – KhoaVo

+0

просто добавьте ': skip_before_filter: authenticate!' в контроллер, где определены действия входа и регистрации. –

+0

Означает ли это, что разработчик не будет аутентифицировать вход в систему и зарегистрировать действия? – KhoaVo