Hello, i have a small problem, but i cant find a "nice" solution for it, maybe someone can help. I want to display the IP adress of the client, on every page a certain controller generates. Therefore i edited the layout to display <%= @ip_adress %>. Now i need to set @ip_adress in every single action this controller has via request.env["REMOTE_IP"]. Is there a "nicer" way, like a standard method in ApplicationController or ActionController::Base which get''s called for every action and i can override it and set the @ip_address? Please consider that i am new to Ruby and especially Rail''s. regards, Patrick
Hi Patrick, On 2.11.2005, at 14.51, Patrick Gebhardt wrote:> Hello, > > i have a small problem, but i cant find a "nice" solution for it, > maybe someone can help. > > I want to display the IP adress of the client, on every page a certain > controller generates. > Therefore i edited the layout to display <%= @ip_adress %>. Now i need > to set @ip_adress in every single action this controller has via > request.env["REMOTE_IP"]. > > Is there a "nicer" way, like a standard method in > ApplicationController or ActionController::Base which get''s called for > every action and i can override it and set the @ip_address?Yes, there is. Use before_filter [1] to set the @ip_address instance attribute on every request: class FooController < ApplicationController before_filter :set_ip private def set_ip @ip_address = request.env["REMOTE_IP"]. end end You can of course do the same in App controller so every controller will inherit it. You can also limit the filter to certain actions by using the :only and :except parameters. hth, //jarkko [1] http://rails.rubyonrails.com/classes/ActionController/Filters/ ClassMethods.html#M000123> > Please consider that i am new to Ruby and especially Rail''s. > > regards, > Patrick > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Nov 2, 2005, at 4:51 AM, Patrick Gebhardt wrote:> I want to display the IP adress of the client, on every page a certain > controller generates. > Therefore i edited the layout to display <%= @ip_adress %>. Now i need > to set @ip_adress in every single action this controller has via > request.env["REMOTE_IP"]. > > Is there a "nicer" way, like a standard method in > ApplicationController or ActionController::Base which get''s called for > every action and i can override it and set the @ip_address?Yes: use request.remote_ip. It even checks for proxied addresses for you! jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDaQznAQHALep9HFYRAloUAKCKNJ82rGPXUHflQMDZxyuPpEG88QCgs6B3 yVfcDJ7fevUz7b6Cp7u9p7k=u6iP -----END PGP SIGNATURE-----
Thanks for all the answers, it works fine now :) 2005/11/2, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org>:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Nov 2, 2005, at 4:51 AM, Patrick Gebhardt wrote: > > I want to display the IP adress of the client, on every page a certain > > controller generates. > > Therefore i edited the layout to display <%= @ip_adress %>. Now i need > > to set @ip_adress in every single action this controller has via > > request.env["REMOTE_IP"]. > > > > Is there a "nicer" way, like a standard method in > > ApplicationController or ActionController::Base which get''s called for > > every action and i can override it and set the @ip_address? > > Yes: use request.remote_ip. It even checks for proxied addresses for > you! > > jeremy > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2 (Darwin) > > iD8DBQFDaQznAQHALep9HFYRAloUAKCKNJ82rGPXUHflQMDZxyuPpEG88QCgs6B3 > yVfcDJ7fevUz7b6Cp7u9p7k> =u6iP > -----END PGP SIGNATURE----- > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Interpunktion und Orthographie dieser Email ist frei erfunden. Eine Übereinstimmung mit aktuellen oder ehemaligen Regeln wäre rein zufällig und ist nicht beabsichtigt. Wer Rechtschreibfehler findet, darf sie behalten.