简体   繁体   English

我如何在Rails中获得客户的IP地址?

[英]How do I get a client's ip address in Rails?

I have a function in my controller that calls another function form the model that needs to be fed the ip address 我的控制器中有一个函数,该函数从需要输入IP地址的模型中调用另一个函数

  def get_location_users
    if current_user
      return current_user.location_users
    else

      l = Location.find_by_ip(request.remote_ip)

      lu = LocationUser.new({:location_id => l.id, :radius => Setting.get("default_radius").to_i})
      return [lu]
    end
  end

from what I gathered remote.request_ip gives you the ip address, however when I call that function with request.remote_ip the object is nil. 从我收集到的信息中,remote.request_ip会为您提供IP地址,但是当我使用request.remote_ip调用该函数时,该对象为nil。 If I put in a static ip address though it produces the right output. 如果我输入一个静态IP地址,尽管它会产生正确的输出。 What would be the correct way to get the ip address if remote.request_ip does not do so? 如果remote.request_ip不这样做,获取IP地址的正确方法是什么?

also when I try typing "request.remote_ip" in the console it returns "undefined local variable or method "request" from main" 当我尝试在控制台中键入“ request.remote_ip”时,它也会从“ main”返回“未定义的局部变量或方法” request”

Do you have a typo in your question or are you really calling remote.request_ip? 您的问题是否有错字,或者您真的在打电话给remote.request_ip?

The correct method would be request.remote_ip 正确的方法是request.remote_ip

This looks like code that should be in a model, so I'm assuming that's where this method is. 这看起来像应该在模型中的代码,所以我假设这就是该方法所在。 If so, you can't (at least 'out of the box') access the request object from your model as it originates from an HTTP request -- this is also why you get "undefined local variable or method "request" from main" in your console. 如果是这样,您将无法(至少是“开箱即用”)访问模型中的请求对象,因为它源自HTTP请求-这也是为什么从main获取“未定义的局部变量或方法” request”的原因在控制台中。

If this method is not already in your model I would place it there, then call it from you controller and pass in request.remote_ip as an argument. 如果您的模型中尚未使用此方法,则可以将其放在此处,然后从您的控制器中调用它,并将request.remote_ip作为参数传递。

def get_location_users(the_ip)
  if current_user
    return current_user.location_users
  else
    l = Location.find_by_ip(the_ip)
    lu = LocationUser.new({:location_id => l.id, :radius => Setting.get("default_radius").to_i})
    return [lu]
  end
end

Then, in your controller :: 然后,在您的控制器中::

SomeModel.get_location_users(request.remote_ip)

Also, be aware that "Location.find_by_ip" will return nil if no records are matched. 另外,请注意,如果没有匹配的记录,“ Location.find_by_ip”将返回nil。

And, you can issue a request in your console by using app.get "some-url" , then you can access the request_ip from request object app.request.remote_ip and use it for testing if needed. 并且,您可以使用app.get“ some-url”在控制台中发出请求,然后可以从请求对象app.request.remote_ip访问request_ip,并在需要时使用它进行测试。

  • HTTP requests: request.ip (as sebastianh pointed out in his answer) HTTP请求: request.ip (如sebastianh在其答案中指出的)

    also available as: request.env['action_dispatch.request_id'] 也可以作为: request.env['action_dispatch.request_id']

  • HTTPS requests: request.env['HTTP_X_FORWARDED_FOR'].split(/,/).try(:first) HTTPS请求: request.env['HTTP_X_FORWARDED_FOR'].split(/,/).try(:first)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM