简体   繁体   中英

Want to call method that will execute everytime, when visitor visit my app rails

Want to call method that will execute everytime, when visitor visit my app rails. Where should I define it, tried in application controller.

def get_ip
   if request.remote_ip == '127.0.0.1'
       # Hard coded remote address
       '123.45.67.89'
   else
        request.remote_ip
   end  
end  

You were doing right, you should define this method in ApplicationController only and call that in before_action . like:

class ApplicationController < ActionController::Base
  before_action :get_ip

  def get_ip
    if request.remote_ip == '127.0.0.1'
      # Hard coded remote address
      '123.45.67.89'
    else
      request.remote_ip
   end
  end
end

get_ip method will call every time as soon as request hits to ApplicationController or any action of any controller inherited from ApplicationController .

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