Undefined method `each' for ***:Class PROBLEM

Hi,

i am very new to ROR, this is my 2nd day since i solve the problem of
database connection. when i started to write some codes, i got stuck.
here is the situation and please give me some help.

i have a “posts” table in my database

CREATE TABLE mapapp.posts (
id int(11) NOT NULL auto_increment,
title varchar(255) default NULL,
body text,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

i have an empty “post.rb” in my Models which looks like this:
class Post < ActiveRecord::Base
end

i wrote an action in my controller which looks like this:
def testing
@posts = Post
for p in @posts
render :text => p.id
end
end

when i run this action on the browser, it gives me this error message:
NoMethodError in PlcController#testing
undefined method `each’ for Post:Class
RAILS_ROOT: ./script/…/config/…

Application Trace | Framework Trace | Full Trace
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
base.rb:1235:in method_missing' #{RAILS_ROOT}/app/controllers/plc_controller.rb:40:intesting’
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
base.rb:1235:in method_missing' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:1095:insend’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:1095:in perform_action_without_filters' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:632:incall_filter’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
filters.rb:619:in perform_action_without_benchmark' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:66:inperform_action_without_rescue’
C:/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:66:inperform_action_without_rescue’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
rescue.rb:83:in perform_action' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:430:insend’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:430:in process_without_filters' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:624:inprocess_without_session_management_support’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
session_management.rb:114:in process' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:330:inprocess’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in
dispatch' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb: 113:inhandle_dispatch’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:in
service' C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' C:/ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start' C:/ruby/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
C:/ruby/lib/ruby/1.8/webrick/server.rb:95:in start' C:/ruby/lib/ruby/1.8/webrick/server.rb:92:ineach’
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in start' C:/ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
C:/ruby/lib/ruby/1.8/webrick/server.rb:82:in start' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:indispatch’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/
webrick.rb:59
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:495:in require' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:342:innew_constants_in’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:495:in require' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39 C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' script/server:3 C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:1235:inmethod_missing’
#{RAILS_ROOT}/app/controllers/plc_controller.rb:40:in testing' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:1095:insend’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:1095:in perform_action_without_filters' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:632:incall_filter’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
filters.rb:619:in perform_action_without_benchmark' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:66:inperform_action_without_rescue’
C:/ruby/lib/ruby/1.8/benchmark.rb:293:in measure' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:66:inperform_action_without_rescue’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
rescue.rb:83:in perform_action' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:430:insend’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:430:in process_without_filters' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:624:inprocess_without_session_management_support’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
session_management.rb:114:in process' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:330:inprocess’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in
dispatch' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb: 113:inhandle_dispatch’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:in
service' C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' C:/ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start' C:/ruby/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
C:/ruby/lib/ruby/1.8/webrick/server.rb:95:in start' C:/ruby/lib/ruby/1.8/webrick/server.rb:92:ineach’
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in start' C:/ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
C:/ruby/lib/ruby/1.8/webrick/server.rb:82:in start' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:indispatch’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/
webrick.rb:59
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:495:in require' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:342:innew_constants_in’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:495:in require' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39 C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`require’
script/server:3

i m very new, so please excuse me if i asked stupid questions.

but thanks for you time looking.

[email protected] wrote:

undefined method `each’ for Post:Class
Your:

 for p in @posts

is equivalent to:

 @posts.each do |p|

You’ve just set @posts to be equal to the class Post, which doesn’t have
an each method.

What you probably meant to do is:

 @posts = Post.find(:all)

or some other query to retrieve an array of Posts.

Pete Y.
http://notahat.com/

Make the following change…

def testing
@posts = Post.find(:all)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs