at.
at.

Reputation: 52580

How to record the IP address a user signed up with using Devise and Ruby on Rails?

I tried the following:

In user.rb:

before_create :record_sign_up_ip

protected
def record_sign_up_ip
  self.sign_up_ip = current_sign_in_ip
end

Unfortunately, although current_sign_in_ip has my correct local ip of 127.0.0.1, sign_up_ip remains as nil. How can I easily get the IP of someone signing up? Normally I would put this in the create controller, but with Devise, there is no controller for User.

Upvotes: 2

Views: 2219

Answers (2)

nachbar
nachbar

Reputation: 2843

Override the controller used by Devise as noted at Override devise registrations controller

As modified from that post, create app/controllers/users/registrations_controller.rb:

# app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController

  def create
    params['user']['sign_up_ip'] = request.env['REMOTE_ADDR']   
    super
  end

end 

Also modified from that post, to use that controller:

# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "users/registrations"}

You also need to make sign_up_up accessible (for Rails 3):

# app/models/user.rb
  attr_accessible :sign_up_ip

Upvotes: 3

Raj
Raj

Reputation: 22956

In your user model,

def init
    self.sign_up_ip = current_sign_in_ip
end

Upvotes: 0

Related Questions