Ruby on Rails is complaining about a method that doesn't exist that is built into Active Record. Wha
- by grg-n-sox
This will probably just be a simple problem and I am just blind or an
idiot but I could use some help.
So I am going over some basic guides in Rails, reviewing the basics
and such for an upcoming exam. One of the guides included was the
sort-of-standard getting started guide over at guide.rubyonrails.org.
Here is the link if you need it. Also all my code is for my app is
from there, so I have no problem releasing any of my code since it
should be the same as shown there. I didn't do a copy paste, but I
basically was typing with Vim in one half of my screen and the web
page in the other half, typing what I see.
http://guides.rubyonrails.org/getting_started.html
So like I said, I am going along the guide when I noticed past a
certain point in the tutorial, I was always getting an error on the
site. To find the section of code, just hit Ctrl+f on the page (or
whatever you have search/find set to) and enter "accepts_". This
should immediately direct you to this chunk of code.
class Post < ActiveRecord::Base
validates_presence_of :name, :title
validates_length_of :title, :minimum => 5
has_many :comments
has_many :tags
accepts_nested_attributes_for :tags, :allow_destroy => :true ,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
So I tried putting this in my code. It is in
~/Rails/blog/app/models/post.rb in case you are wondering. However,
even after all the other code I put in past that in the guide, hoping
I was just missing some line of code that would come up later in the
guide. But nothing, same error every time. This is what I get.
NoMethodError in PostsController#index
undefined method `accepts_nested_attributes_for' for #<Class:0xb7109f98>
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:1833:in
`method_missing'
app/models/post.rb:7
app/controllers/posts_controller.rb:9:in `index'
Request
Parameters:
None
Response
Headers:
{"Content-Type"=>"",
"cookie"=>[],
"Cache-Control"=>"no-cache"}
Now, I copied the above code from the guide. The two code sections I
edited mentioned in the error message I will paste as is below.
class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
before_filter :find_post,
:only => [:show, :edit, :update, :destroy]
def index
@posts = Post.find(:all) # <= the line 9 referred to in error message
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
class Post < ActiveRecord::Base
validates_presence_of :name, :title
validates_length_of :title, :minimum => 5
has_many :comments
has_many :tags
accepts_nested_attributes_for :tags, :allow_destroy => :true , # <= problem
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
Also here is gem local gem list. I do note that they are a bit out of date, but the default Rails install any of the school machines (an environment likely for my exam) is basically 'gem install rails --version 2.2.2' and since they are windows machines, they come with all the normal windows ruby gems that comes with the ruby installer. However, I am running this off a Debian virtual machine of mine, but trying to set it up similarly and I figured the windows ruby gems wouldn't change anything in Rails.
*** LOCAL GEMS ***
actionmailer (2.2.2)
actionpack (2.2.2)
activerecord (2.2.2)
activeresource (2.2.2)
activesupport (2.2.2)
gem_plugin (0.2.3)
hpricot (0.8.2)
linecache (0.43)
log4r (1.1.7)
ptools (1.1.9)
rack (1.1.0)
rails (2.2.2)
rake (0.8.7)
sqlite3-ruby (1.2.3)
So any ideas on what the problem is? Thanks in advanced.