Forum: Ruby need help to fix syntax problem for cgi

Posted by Ferdous ara (ferdous)
on 2012-10-02 14:47
Hi bellow is the script.

I think its failing at

if #{f['name']} =~/asw1-v2r7c9-bry/
                brydc << #{f['name']}
        end


but dont understand where is the problem , i just get Internal Server
Error if i enable
this line   brydc << #{f['name']}

I am quite new and i am from php background.. and in php it was easy to
troubleshoot ..


#!/usr/bin/ruby
require 'cgi'
require 'mysql'
cgi = CGI.new

m = Mysql.new("localhost","root","","report")
r = m.query("select device.name as name, hardware.name as model
device.hardware=hardware.id")
brydc=Array.new
r.each_hash do |f|
        if #{f['name']} =~/asw1-v2r7c9-bry/
                brydc << #{f['name']}
        end

end
puts cgi.header

puts "<html>
<!DOCTYPE html>
<html>
<head>
<title> Per DataCenter </title>
<link rel='stylesheet' href='style.css' type='text/css' />
</head><body>

<h1>Switch Per DataCenter  </h1>
</div>
<div>
<table border='1'>"

brydc.each do |f|
puts "<tr>"
puts "<td>#{f['name']}</td>"
puts "</tr>"
end

puts "</table>


</div>

</body></html>"

thanks for your help.
Posted by unknown (Guest)
on 2012-10-02 15:23
(Received via mailing list)
Am 02.10.2012 14:47, schrieb Ferdous ara:
> Hi bellow is the script.
      ^^^^^^

> I think its failing at
>
> if #{f['name']} =~/asw1-v2r7c9-bry/
>                  brydc << #{f['name']}
>          end

You should use irb to try out code snippets.

1.9.3-p194 :001 > value = 'a hash value'
  => "a hash value"
1.9.3-p194 :002 > array = []
  => []
1.9.3-p194 :003 > array << #{value}
1.9.3-p194 :004 >   puts array
SyntaxError: (irb):4: syntax error, unexpected tIDENTIFIER, expecting
keyword_do or '{' or '(' from ......./irb:16:in `<main>'


#{} is supposed to be used inside double quotes.
Assuming your hash values are strings, just remove it.

   if f['name'] =~ /asw1-v2r7c9-bry/
     brydc << f['name']
   end
Posted by 7stud -- (7stud)
on 2012-10-02 17:38
> I am quite new and i am from php background.. and in php it was easy to
>  ..

The ruby error will be in your web server's log file.  It looks like you
are using a unix operating system, so you can tail the log file:

$ tail -f 'log_file_name'

...which will show the last lines from the log file and update them in
real time as new lines are written to the log file.


Or, you can use something like here:

a)  http://rubygems.org/gems/cgi-exception

or

b) http://eradman.com/posts/ruby-cgi.html
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.