#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    10
    Rep 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.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    19
    Rep 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

    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Instance variable


    Even if this thread isn't that new, it may help some other users:
    Ruby separates between local and instance variables by the given name. Local variables start with a letter or often with a '_' and can be addressed only within the class. Instance variables start with the '@'-symbol and can be addressed from outside the class. So try it with an '@send_this' (both in the controller and in the view) and it should work.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0
    Originally Posted by rubyist
    Even if this thread isn't that new, it may help some other users:
    Ruby separates between local and instance variables by the given name. Local variables start with a letter or often with a '_' and can be addressed only within the class. Instance variables start with the '@'-symbol and can be addressed from outside the class. So try it with an '@send_this' (both in the controller and in the view) and it should work.
    When rubyist says "class", he means "method or block". Instance variable are available throughout the class.

IMN logo majestic logo threadwatch logo seochat tools logo