Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby cgi不会返回方法调用,但会返回参数_Ruby_Cgi_Return_If Statement - Fatal编程技术网

ruby cgi不会返回方法调用,但会返回参数

ruby cgi不会返回方法调用,但会返回参数,ruby,cgi,return,if-statement,Ruby,Cgi,Return,If Statement,我的环境:ruby 1.9.3p392(2013-02-22修订版39386)[x86_64-linux] 问题是,我可以让ruby返回通过GET发送的参数。但是,当我试图在if/else中将它们用作我的方法的参数时,ruby不会返回任何东西,结果是一个空白页 ph和pm返回正确: http://127.0.0.1/cgi-bin/test.rb?hostname=node00.abit.dk&macadd=23:14:41:51:63 返回: node00.abit.dk 23:14

我的环境:ruby 1.9.3p392(2013-02-22修订版39386)[x86_64-linux]

问题是,我可以让ruby返回通过GET发送的参数。但是,当我试图在if/else中将它们用作我的方法的参数时,ruby不会返回任何东西,结果是一个空白页

ph和pm返回正确:

http://127.0.0.1/cgi-bin/test.rb?hostname=node00.abit.dk&macadd=23:14:41:51:63
返回:

node00.abit.dk 23:14:41:51:63
与数据库(MySQL)的连接工作正常

当我测试方法newHostName时,它会正确输出:

puts newHostName
返回(哪个是正确的)

守则:

#!/usr/bin/ruby
require 'cgi'
require 'sequel'
require 'socket'
require 'timeout'

DB = Sequel.connect(:adapter=>'mysql', :host=>'localhost', :database=>'nodes', :user=>'nodeuser', :password=>'...')

#cgi-part to work
#takes 2 parameters:
#hostname & macadd 
cgi = CGI.new
puts cgi.header

p = cgi.params
ph = p['hostname']
pm = p['macadd']

def nodeLookup(hostnameargv)
        hostname = DB[:basenode]
        h = hostname[:hostname => hostnameargv]

        h1 = h[:hostname]
        h2 = h[:macadd]
        ary = [h1, h2]
        return ary
end

def lastHostName()
    #TODO: replace with correct sequel-code and NOT raw SQL
    DB.fetch("SELECT hostname FROM basenode ORDER BY id DESC LIMIT 1") do |row|
        return row[:hostname]
    end
end

def newHostName()
    org = lastHostName
    #Need this 'hack' to make ruby grep for the number
    #nodename e.g 'node01.abit.dk'
    var1 = org[4]
    var2 = org[5]
    var3 = var1 + var2
    sum = var3.to_i + 1
    #puts sum 
    sum = "node" + sum.to_s + ".abit.dk"
    return sum
end

def insertNewNode(newhost, newmac)
    newnode = DB[:basenode]
    newnode.insert(:hostname => newhost, :macadd => newmac)

    return "#{newnode.count}"
end

#puts ph
#puts pm

#puts newHostName
cgi.out() do
    cgi.html do
        begin
            if ph == "node00.abit.dk"
                puts newHostName
            else
                puts nodeLookup(ph)
            end
        end
    end
end
我觉得我错过了一些东西。非常感谢您的帮助


//M00kaw

如何修改代码的最后几行,如下所示?CGI HTML生成方法接受一个块,并生成该块的返回值作为其内容。因此,您应该将
newHostName
nodeLookup(ph)
作为传递给
cgi.html()
的块的返回值,而不是
put sth
,后者将内容打印到您的终端并返回
nil
。这就是为什么
cgi.html()

#放入新主机名
out()做什么
cgi.html
如果ph==“node00.abit.dk”
新主机名
其他的
nodeLookup(ph)
结束
结束
结束

p.s.常规做法是用两个空格缩进ruby代码:-)

谢谢你的帮助:>我要解决两个空格的问题。。从if/else中删除“put”没有帮助。它仍然没有输出任何内容:/查看cgi服务器的日志文件。你可能会在那里发现一些有用的东西。或者,您可以尝试在调试时不依赖数据库,只需从
cgi.out()
返回一些硬编码字符串,看看是否有效。数据库连接可能有问题。
#!/usr/bin/ruby
require 'cgi'
require 'sequel'
require 'socket'
require 'timeout'

DB = Sequel.connect(:adapter=>'mysql', :host=>'localhost', :database=>'nodes', :user=>'nodeuser', :password=>'...')

#cgi-part to work
#takes 2 parameters:
#hostname & macadd 
cgi = CGI.new
puts cgi.header

p = cgi.params
ph = p['hostname']
pm = p['macadd']

def nodeLookup(hostnameargv)
        hostname = DB[:basenode]
        h = hostname[:hostname => hostnameargv]

        h1 = h[:hostname]
        h2 = h[:macadd]
        ary = [h1, h2]
        return ary
end

def lastHostName()
    #TODO: replace with correct sequel-code and NOT raw SQL
    DB.fetch("SELECT hostname FROM basenode ORDER BY id DESC LIMIT 1") do |row|
        return row[:hostname]
    end
end

def newHostName()
    org = lastHostName
    #Need this 'hack' to make ruby grep for the number
    #nodename e.g 'node01.abit.dk'
    var1 = org[4]
    var2 = org[5]
    var3 = var1 + var2
    sum = var3.to_i + 1
    #puts sum 
    sum = "node" + sum.to_s + ".abit.dk"
    return sum
end

def insertNewNode(newhost, newmac)
    newnode = DB[:basenode]
    newnode.insert(:hostname => newhost, :macadd => newmac)

    return "#{newnode.count}"
end

#puts ph
#puts pm

#puts newHostName
cgi.out() do
    cgi.html do
        begin
            if ph == "node00.abit.dk"
                puts newHostName
            else
                puts nodeLookup(ph)
            end
        end
    end
end