views:

85

answers:

2

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.

+3  A: 

If you look on http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for you'll see that accepts_nested_attributes_for only exists since Rails 2.3.2, so I'm afraid you won't have that functionality if you're running off of 2.2.

Antonio Salazar Cardozo
Thanks! Also, if you don't me asking, can you possibly see any major drawbacks from just updating to the current stable? The professor did mention we can declare a version of Rails so long as it is not Rails 3. Just trying to figure out if I should just leave it at Rails 2.2.2 since that is the default setup on any school computer and just avoid stuff like that, update to 2.3.2 so I have the needed functionality to follow the guides used as references in the class, or just do a full update to whatever is the current stable version?
grg-n-sox
If you're just starting up, go with the latest version. Rails releases carry a lot of good stuff in them every time.You can avoid dealing with issues with other computers not having the gem by doing a rake rails:freeze:gems, which will effectively copy the gems into your vendor/rails directory and use those instead of locally installed gems.
Antonio Salazar Cardozo
Nice to know about the gem freezing. Thanks again! And thanks a lot!
grg-n-sox
No problem, glad to help :)
Antonio Salazar Cardozo
+1  A: 

You must upgrade to 2.3.2 or higher as Antonio suggests. I suggest using 2.3.5. You can either upgrade the gems on the system or freeze Rails to your application.

To upgrade the gems

gem install rails -v=2.3.5

To freeze Rails

cd /your/app/
git clone git://github.com/rails/rails vendor/rails
cd vendor/rails
git checkout v2.3.5
Ryan Bigg