在 Ruby on Rails 中更改 request.remote_ip 的值 [英] Change value of request.remote_ip in Ruby on Rails

查看:40
本文介绍了在 Ruby on Rails 中更改 request.remote_ip 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于测试目的,我想更改 request.remote_ip 的返回值.在我的开发机器上,它总是返回 127.0.0.1 ,但我想给自己不同的假 IP 来测试我的应用程序的正确行为,而无需先将其部署到实时服务器!

For test purposes I want to change the return value of request.remote_ip. While being on my development machine it returns always 127.0.0.1 as it should but I would like to give myself different fake IPs to test the correct behavior of my app without deploying it to an live server first!

谢谢.

推荐答案

如果您希望在整个应用程序中使用此功能,在您的 app/helpers/application_helper.rb 中覆盖 remote_ip 方法可能更好/更容易:

If you want this functionality in your whole application, it might be better/easier to override the remote_ip method in your app/helpers/application_helper.rb:

class ActionDispatch::Request #rails 2: ActionController::Request
  def remote_ip
    '1.2.3.4'
  end
end

而且1.2.3.4地址随处可见

And the 1.2.3.4 address is available everywhere

这篇关于在 Ruby on Rails 中更改 request.remote_ip 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆