如何使用Devise和Ruby on Rails记录用户注册的IP地址? [英] How to record the IP address a user signed up with using Devise and Ruby on Rails?
问题描述
我尝试了以下内容:
在 user.rb
中:
before_create :record_sign_up_ip
protected
def record_sign_up_ip
self.sign_up_ip = current_sign_in_ip
end
不幸的是,虽然 current_sign_in_ip
有我正确的本地ip 127.0.0.1
, sign_up_ip
保持为 nil
。如何轻松获得有人注册的IP?通常我会把它放在创建
控制器中,但是使用Devise,没有控制器用于 User
。
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
.
推荐答案
覆盖Devise使用的控制器,如覆盖设计注册控制器
Override the controller used by Devise as noted at Override devise registrations controller
从该帖子修改后,创建app / controllers / users / registrations_controller.rb:
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"}
您还需要使sign_up_up可访问(对于Rails 3):
You also need to make sign_up_up accessible (for Rails 3):
# app/models/user.rb
attr_accessible :sign_up_ip
这篇关于如何使用Devise和Ruby on Rails记录用户注册的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!