ruby - Rails devise after_sign_in first time, check attribute and update another user one time -


after user's first sign in, want see if user referred them referral_code_used column, , increase user's referral_count + 1. currently, user logged in , taken home_path

i have tried writing after_sign_in_path_for(resource) method. gets job done, respect referral count. issue takes user users/:id path instead of home.

def after_sign_in_path_for(resource)      if resource.sign_in_count > 1      puts "sign in count greater 1"     elsif resource.sign_in_count === 1       puts "sign in count 1"      if resource.referral_code_used != nil            referral_code = resource.referral_code.strip       length = referral_code.length       referral_code = referral_code[5..length]       @user_referral_code = referral_code.to_i         if user.exists?(id: @user_referral_code)         referral_user = user.find_by_id(@user_referral_code)         referral_user.referral_count = referral_user.referral_count + 1         referral_user.save       end      end      puts "signin count = 1"    end    #redirect_to home_path   end 

when put in redirect_to home_path, says render and/or redirect called multiple times in action. i'm guessing in new sessions controller, handled devise, unsure that.

when put in application controller, below, there no routing issues, calls multiple times , adds more 1 referral_count:

 before_action :check_referral_code     def check_referral_code     if user_signed_in?         if current_user.sign_in_count > 1          puts "sign in count greater 1"         elsif current_user.sign_in_count === 1           puts "sign in count 1"          if current_user.referral_code_used != nil                referral_code = current_user.referral_code_used.strip           length = referral_code.length           referral_code = referral_code[5..length]           @user_referral_code = referral_code.to_i             if user.exists?(id: @user_referral_code)             referral_user = user.find_by_id(@user_referral_code)             referral_user.referral_count = referral_user.referral_count + 1             referral_user.save           end          end          puts "signin count = 1"        end     end    end 

relevant routes:

devise_for :users, controllers: { registrations: "users/registrations" } resources :users 'profile/home', to: 'profile#home', as: 'home'  

what correct way check referral_code_used on first sign in, , credit other user 1 time? thanks!

try replacing redirect_to home_path home_path

def after_sign_in_path_for(resource)   if resource.sign_in_count > 1     puts "sign in count greater 1"   elsif resource.sign_in_count === 1     puts "sign in count 1"     if resource.referral_code_used != nil            referral_code = resource.referral_code.strip       length = referral_code.length       referral_code = referral_code[5..length]       @user_referral_code = referral_code.to_i         if user.exists?(id: @user_referral_code)         referral_user = user.find_by_id(@user_referral_code)         referral_user.referral_count = referral_user.referral_count + 1         referral_user.save       end     end     puts "signin count = 1"   end  home_path end 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -