新手问题 rails 的 helper 中怎么获取当前服务器的 ip 或者 hostname?

baypm2014 · December 08, 2014 · Last by zgm replied at December 08, 2014 · 2742 hits

rails 的 helper 中怎么获取当前服务器的 ip 或者 hostname? request.ip() 只能在 controller 中用。

在 controller 中把它封装成一个 helper_method,就跟 current_user 一样

这个问题我遇到过。比如 model 里要写一个 save_ip() 方法。

结论是:只能把 ip 当参数传过去。或者把 request 传过去。

我试了 helper 里面可以用 request.ip()

当前服务器是当前请求设备的 ip, 还是当前处理服务器的 ip 呢?

You need to Sign in before reply, if you don't have an account, please Sign up first.