Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old May 18th, 2007, 10:01 PM
lotus_anima lotus_anima is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 2 lotus_anima User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 25 sec
Reputation Power: 0
ERB "undefined local variable or method"

Here is a snippet of my Ruby...

Code:
  def serve_html(sock)
    browser = sock
    
    template = ERB.new(load_template_as_string('test.rhtml'), 0, '%<>')
    send_this = "Hello, world!"
    
    
    browser.write("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
    browser.write(template.result)
    
    close_socket(sock)
  end


And here is a snippet from test.rhtml...
Code:
<body><%= send_this %></body>


But I get...
Code:
(erb):10: undefined local variable or method `send_this' for main:Object (NameError)


What gives? Mine is nearly identical to the ERB docs.

Reply With Quote
  #2  
Old May 27th, 2007, 09:13 AM
voipfc voipfc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 19 voipfc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 49 m 40 sec
Reputation Power: 0
listing
PHP Code:
require 'erb'

  
def serve_html(file)

    
send_this "Hello world 2"
    
data File.read(file)
    
template ERB.new(data0'%<>')
    
send_this "Hello, world 3!"

    
puts send_this
    puts
("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
    
puts(template.result)


  
end
send_this
="Hello world 1!"
serve_html('test.rhtml'


execution

Quote:
Hello, world 3!
HTTP/1.0 200 OK
Content-type: text/html

<body>Hello world 1!</body>

Comment out the send_this="Hello world 1" at the end of the listing and the error will appear. It appears that when the puts is xecuted template.result is out of scope.

It has something to do with bindings - I am new to ruby - don't ask me why.

This is what I used to get your code to work. It involves something known as bindings and is in the ERb examples
PHP Code:
require 'erb'

  
def serve_html(file)
    
binding
    send_this 
"Hello world"
    
data File.read(file)
    
template ERB.new(data0'%<>')

    
puts("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
    
puts(template.result(b))


  
end
serve_html
('test.rhtml'

Note the bracketed (b) in the template.result call.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > ERB "undefined local variable or method"


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway