2010-05-12 35 views
0

我正在閱讀Rails中的一些基本指南,查看即將到來的考試的基礎知識 等。其中包括的指南之一是在guide.rubyonrails.org上的 排序標準入門指南。 這裏是鏈接,如果你需要它。此外,我所有的代碼都是從我的應用程序 ,所以我沒有任何問題,因爲它的 應該與那裏顯示的相同。我沒有做一個複製粘貼,但我基本上是在Vim的一半屏幕和另一半的網頁 中打字,輸入我看到的內容。Ruby on Rails抱怨內置於Active Record中的方法不存在。什麼?

http://guides.rubyonrails.org/getting_started.html

所以就像我說的,我沿着導時,我注意到在過去的教程 某一點去,我總是在 網站得到一個錯誤。要查找代碼段,只需在頁面上點擊Ctrl + f(或者搜索/查找設置爲 ),然後輸入「accepters_」。這個 應該立即引導你到這塊代碼。

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 

所以我試着把它放在我的代碼中。這是在 〜/ Rails /博客/應用程序/模型/ post.rb萬一你想知道。但是,即使在指南中提及的所有其他代碼之後,我仍然錯過了 ,但我錯過了稍後在 指南中出現的一些代碼行。但每次都是一樣的錯誤。這就是我得到的。


NoMethodError在PostsController#索引

未定義的方法`accepts_nested_attributes_for」爲#<類別: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'

請求

參數:

響應

接頭:

{ 「內容類型」= > 「」, 「曲奇」= > [], 「Cache-Control」= >「no-cache」}


現在,我從指南中複製了上面的代碼。在錯誤消息I中提到的編輯的兩個代碼段I 我將粘貼,如下所示。


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 

而且,這裏是當地的寶石寶石列表。我注意到它們有點過時了,但默認的Rails安裝了任何學校機器(一個可能用於我的考試的環境)基本上是'gem install rails --version 2.2.2',並且由於它們是Windows機器,他們帶有ruby安裝程序附帶的所有常規Windows ruby​​寶石。然而,我正在使用Debian虛擬機運行這個程序,但試圖設置它,我認爲Windows ruby​​寶石不會改變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) 

回答

3

如果你看看http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for你會看到accepts_nested_attributes_for只存在,因爲線路2.3.2,所以我怕,如果你逃跑的2.2你不會有這個功能。

+0

謝謝!另外,如果你不問我,你是否可以看到僅僅更新到目前的穩定版有什麼重大缺陷?教授確實提到我們可以聲明一個Rails版本,只要它不是Rails 3即可。只是想知道我是否應該將它留在Rails 2.2.2中,因爲這是任何學校計算機上的默認設置,並且只是避免了東西就像那樣,更新到2.3.2,所以我有必要的功能來跟隨作爲類中引用的指南,或者只是對當前穩定版本進行全面更新? – 2010-05-12 20:29:03

+0

如果你剛開始使用最新版本。 Rails發佈每次都會帶來很多好東西。 您可以避免通過執行rake rails來處理與沒有gem的其他計算機相關的問題:freeze:gems,它將有效地將gem複製到供應商/ rails目錄中,並使用那些而不是本地安裝的gem。 – 2010-05-12 20:51:24

+0

很高興知道寶石凍結。再次感謝!並且非常感謝! – 2010-05-12 21:00:14

1

根據Antonio的建議,您必須升級到2.3.2或更高版本。我建議使用2.3.5。您可以升級系統上的寶石或凍結Rails到您的應用程序。

要升級寶石

gem install rails -v=2.3.5 

要凍結的Rails

cd /your/app/ 
git clone git://github.com/rails/rails vendor/rails 
cd vendor/rails 
git checkout v2.3.5 
相關問題