Forum: Ruby on Rails Newbie stumped by "NameError in "

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David T. (Guest)
on 2006-02-14 06:34
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)
<!-- This is eventually just an html fragment  -->
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!)
<!-- This is to create a connection to MindReader server  -->
<!-- Right now the code doesn't matter, because I cannot  -->
<!-- get this section of code even called.  -->
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)
<!--    Section that displays images -->
            <div id="mindreadrread_results">The images should go here
after clicking "click here"</div>
        <%= 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
Byron S. (Guest)
on 2006-02-14 06:56
(Received via mailing list)
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
Alan F. (Guest)
on 2006-02-14 13:52
David T-L wrote:

> display_images_from_mindreadr.rhtml (The "<%=" is the source of the
> error)
> <!-- This is eventually just an html fragment  -->
> 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 %>
---------------------
David T. (Guest)
on 2006-02-14 18:24
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
David T. (Guest)
on 2006-02-14 18:27
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)
>> <!-- This is eventually just an html fragment  -->
>> 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 %>
> ---------------------
This topic is locked and can not be replied to.