Sonalkumar sute
Sonalkumar sute

Reputation: 2575

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  

Upvotes: 0

Views: 384

Answers (1)

Kuldeep
Kuldeep

Reputation: 884

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.

Upvotes: 3

Related Questions