当我想要请求IP地址并知道它是什么时,我不确定它是如何工作的。现在我正在开发和rails sandbox控制台中:
public void LinkBuilder(string baselink, string sharedkey, string service, string period, string bulletintype,
string includeresults, string includemap, string username, string password)
{
var allParams = new List<string>
{
baselink,
sharedkey,
service,
period,
bulletintype,
includeresults,
includemap,
username,
password
};
var completeLink = "?" + String.Join("&", allParams.Select(p => p != null));
}
然后它返回:
user$ rails console --sandbox
Loading development environment in sandbox (Rails 4.2.3)
Any modifications you make will be rolled back on exit
irb(main):001:0> ip = request.ip
我不知道这里发生了什么。我试图通过在我的应用程序控制器中执行此操作,让Geocoder将匿名用户的IP地址转换为街道地址:
NameError: undefined local variable or method `request' for main:Object
from (irb):1
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `block in require'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
from /home/user/app/bin/rails:8:in `<top (required)>'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `block in load'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/user/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'irb(main):002:0>
但它并不起作用,因为我猜测没有ip。如何正确请求用户ip?
答案 0 :(得分:0)
在Rails控制台中,您没有请求对象。
如果要调试请求及其IP,则应在控制器或视图级别执行此操作。另外,为了简化它,您应该考虑在其binding.pry
方法中使用Pry控制台。您可以在控制器操作中调用此方法:
def index
binding.pry
# some code here
end
或在视图中使用它:
<% binding.pry %>
答案 1 :(得分:0)