Set user IP on create

i tried to make a callback to assign the user IP address on create

before_create
self.ip = request.remote_ip
end

but I get

NameError (undefined local variable or method `request’ for
#User:0x5b05078):

On 21 July 2011 17:33, Tomas R. [email protected] wrote:

i tried to make a callback to assign the user IP address on create

before_create
self.ip = request.remote_ip
end

but I get

NameError (undefined local variable or method `request’ for
#User:0x5b05078):

I think (though not certain) that request is only available in the
controller and since the callback is in the model then request is not
available.

Colin

Yep. Models don’t (and probably shouldn’t) know about the request at
all.

so how should I add the IP? assign it on the controller or maybe a
hidden text field?

Assigning it in the controller is probably the best bet:

instance = Model.new(params[:model])
instance.ip_address = request.ip_address

Tim S. wrote in post #1012223:

Assigning it in the controller is probably the best bet:

instance = Model.new(params[:model])
instance.ip_address = request.ip_address

I will do it then, thanks

On Jul 21, 2011, at 10:51 AM, Tomas R. wrote:

so how should I add the IP? assign it on the controller or maybe a
hidden text field?

You’ll have access to the request in the controller.