# Newbie stumped by "NameError in "

**URL:** <https://www.ruby-forum.com/t/newbie-stumped-by-nameerror-in/53748>\
**Category:** Rails\
**Created:** [February 14, 2006, 5:34am UTC](https://www.ruby-forum.com/t/newbie-stumped-by-nameerror-in/53748 "2006-02-14T05:34:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 14, 2006, 5:34am UTC](https://www.ruby-forum.com/t/newbie-stumped-by-nameerror-in/53748/1 "2006-02-14T05:34:57Z")

</div>

Help. I cannot find any help by searching…

I’m slowly creating an application using tutorial examples, etc, but I  
think I’m missing something real basic:

Here’s the error message:  
NameError in Mindreadr#display\_images\_from\_mindreadr  
undefined local variable or method `mindreadr\_open\_socket’ for  
#\<#Class:0x37c3b08:0x37c3a30\>

Here are what I think are the key files:

display\_images\_from\_mindreadr.rhtml (The “\<%=” is the source of the  
error)

I need to Open a Socket to MindReadr  
\<%= mindreadr\_open\_socket %\>  
Then Request some images  
Then display the images  
(without the \<%= line, I get the other lines as text as I expected)

mindreadr\_open\_socket (I don’t think this is the problem…never gets  
here…if you can see a problem with this code, let me know!)

require “socket”  
STDOUT.flush  
s = TCPSocket.open(“192.168.107.10”, 9000)  
result = “MindReadr Request called”

mindreadr\_controller.rb (mindreader\_open\_socket is defined here)  
class MindreadrController \< ApplicationController  
def index  
end

```
def display_images_from_mindreadr
    # Basic action which calls _open_socket, _request, and _read
    render(:layout => false)
end

def mindreadr_open_socket
    # Connect to MindReader server/port
    render(:layout => false)
end

def mindreadr_request
    # Ask MindReadr for some images
    render(:layout => false)
end

def mindreadr_read
    # Read output from MindReadr
    render(:layout => false)
end

def ajax_example
    render(:layout => false)
end

```

index.rhtml (partial- this all works fine until I hit the action button)

```
        <div id="mindreadrread_results">The images should go here 

```

after clicking “click here”  
\<%= link\_to\_remote(“Click here for images for images to appear  
above”,  
:update =\> ‘mindreadrread\_results’,  
:url =\> { :action =\>  
:display\_images\_from\_mindreadr }) %\>

Something tells me I haven’t declared something in the right place.  
Suggestions for debugging?

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 14, 2006, 5:56am UTC](https://www.ruby-forum.com/t/newbie-stumped-by-nameerror-in/53748/2 "2006-02-14T05:56:37Z")

</div>

Your error is saying that the variable (mindreadr\_open\_socket) you’re  
using  
is not set so when you try to display it it says that there is a  
“undefiined  
local variable or method”

You should also use: @mindreadr\_open\_socket

I’m not sure if this is convention or necessary but I’d do it 🙂

As a quick test just add:  
@mindreadr\_open\_socket = ‘hello world’

as the first line in your “def display\_images\_from\_mindreadr” method.

Don’t forget to update your code to:

\<%= @mindreadr\_open\_socket %\> in the rhtml

- Byron

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 14, 2006, 12:52pm UTC](https://www.ruby-forum.com/t/newbie-stumped-by-nameerror-in/53748/3 "2006-02-14T12:52:15Z")

</div>

David T-L wrote:

> display\_images\_from\_mindreadr.rhtml (The “\<%=” is the source of the  
> error)
> 
> I need to Open a Socket to MindReadr  
> \<%= mindreadr\_open\_socket %\>  
> Then Request some images  
> Then display the images  
> (without the \<%= line, I get the other lines as text as I expected)

Are you trying to call the controller.mindreadr\_open\_socket method from  
the view ? I don’t think that will work. I think, however, if you  
move that method to the helper, it can be called from the view.

Also, unless the method returns a string that you want displayed, you  
need to use \<% %\> rather than \<%= %\>

It may be you want something more like something more like:

— in the helper —

def mindreadr\_open\_socket  
socket = create\_\_and\_open\_the\_socket\_somehow  
end

—in the view—

\<% socketthing = mindreadr\_open\_socket %\>

…blah blah…  
\<% for blah %\>  
\<%= socketthing.get\_data %\>  
\<% end %\>

## \<% sockething.close %\>

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 14, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/newbie-stumped-by-nameerror-in/53748/4 "2006-02-14T17:27:06Z")

</div>

Alan,

I think you are exactly right. I still don’t understand all of the  
scafolding interconnectedness. Thanks for giving me such a complete  
answer. I need to study it and give it a try.

Thanks for taking the time to “solve” my real problem.  
Dave

Alan F. wrote:

> David T-L wrote:
> 
> > display\_images\_from\_mindreadr.rhtml (The “\<%=” is the source of the  
> > error)
> > 
> > I need to Open a Socket to MindReadr  
> > \<%= mindreadr\_open\_socket %\>  
> > Then Request some images  
> > Then display the images  
> > (without the \<%= line, I get the other lines as text as I expected)
> 
> Are you trying to call the controller.mindreadr\_open\_socket method from  
> the view ? I don’t think that will work. I think, however, if you  
> move that method to the helper, it can be called from the view.
> 
> Also, unless the method returns a string that you want displayed, you  
> need to use \<% %\> rather than \<%= %\>
> 
> It may be you want something more like something more like:
> 
> — in the helper —
> 
> def mindreadr\_open\_socket  
> socket = create\_\_and\_open\_the\_socket\_somehow  
> end
> 
> —in the view—
> 
> \<% socketthing = mindreadr\_open\_socket %\>
> 
> …blah blah…  
> \<% for blah %\>  
> \<%= socketthing.get\_data %\>  
> \<% end %\>
> 
> ## \<% sockething.close %\>

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [February 14, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/newbie-stumped-by-nameerror-in/53748/5 "2006-02-14T17:24:16Z")

</div>

Byron,

Thank you. That made the error message go away. I see I have to  
initialize the variable. However, I really thought I was constructing  
an action. So I think I have a deeper problem.

Dave

Byron S. wrote:

> Your error is saying that the variable (mindreadr\_open\_socket) you’re  
> using  
> is not set so when you try to display it it says that there is a  
> “undefiined  
> local variable or method”
> 
> You should also use: @mindreadr\_open\_socket
> 
> I’m not sure if this is convention or necessary but I’d do it 🙂
> 
> As a quick test just add:  
> @mindreadr\_open\_socket = ‘hello world’
> 
> as the first line in your “def display\_images\_from\_mindreadr” method.
> 
> Don’t forget to update your code to:
> 
> \<%= @mindreadr\_open\_socket %\> in the rhtml
> 
> - Byron
