Forum: Ruby Newbie: undefined method

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.
6dbd2f48483293d6ecf9263a7bd4fc97?d=identicon&s=25 tim.hennekey@gmail.com (Guest)
on 2006-03-08 17:24
(Received via mailing list)
I get "undefined method `name' for #<TSPProcess:0x38c7fc0>" when I
access the page with the template:

 <% @processes.each do |asd| %>
  <tr>
   <td><%= render(:inline => "#{process.name}")%></td>
  </tr>
 <% end %>

The TSPProcess.rb is:

class TSPProcess
  @name = "Nothing"
  attr_reader :name
  attr_writer :name
 end

I tried defining name and name= and I get the same error.
The controller:

require 'TSPProcess'
class ProcessesController < ApplicationController
  def index
    @processes = Array.new
    a = TSPProcess.new
    @processes << a
  end
end

Any help for a beginner?
6dbd2f48483293d6ecf9263a7bd4fc97?d=identicon&s=25 tim.hennekey@gmail.com (Guest)
on 2006-03-08 17:30
(Received via mailing list)
The template should read:
 <% @processes.each do |process| %>
  <tr>
   <td><%= render(:inline => "#{process.name}")%></td>
  </tr>
 <% end %>
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-03-08 17:42
(Received via mailing list)
On Mar 8, 2006, at 5:23 PM, tim.hennekey@gmail.com wrote:

> class TSPProcess
>   @name = "Nothing"
>   attr_reader :name
>   attr_writer :name
>  end

The @name could be causing some problems. If you really want a
default of "Nothing", you should move it to the #initialize method.

def initialize
   @name = "Nothing"
end

Try:

class TSPProcess
   attr_accessor :name
end

and in your template:

<% @processes.each do |process| %>
   <tr>
     <td><%= render(:inline => "#{process.name or "Nothing"}")%></td>
   </tr>
<% end %>

> require 'TSPProcess'

You should ruby's convention for file-naming: require 'tsp_process'

> class ProcessesController < ApplicationController
>   def index
>     @processes = Array.new
>     a = TSPProcess.new
>     @processes << a
>   end
> end

You could shorten this to:
class ProcessesController < ApplicationController
   def index
     @processes = [TSPProcess.new]
   end
end

-- Daniel
6dbd2f48483293d6ecf9263a7bd4fc97?d=identicon&s=25 tim.hennekey@gmail.com (Guest)
on 2006-03-08 18:01
(Received via mailing list)
This worked out for me, thank you.
This topic is locked and can not be replied to.