2012-08-06 3 views
0

나는 바보 같은 것을 간과하고 있지만 레일 3 튜토리얼을 따르고 있다고 생각합니다. 사용자가 내가 말하는이 시점에서 다음 코드구문 오류 예기치 않은 레일 3의 tANDOP 튜토리얼

>> article = Article.draft.first 
=> #<Article id: 7, title: "One-to-many associations",  ...> 
>> comment = article.comments.create :name => 'Dude', :email => '[email protected]', :body => 'Great article!' 

를 입력하여 "게시되지 않은"기사 (초안)에 주석을 만들려고 레일 콘솔 그래서 게시되지 않은 문서는 ... 내가 얻을 생각 해요

=> #<Comment id: nil, article_id: 7, name: "Dude", email: "[email protected]", body: "Great article!", created_at: nil, updated_at: nil> >> comment.errors.full_messages => ["Article is not published yet"] 

을하지만 그 대신 나는 다음과 같은 오류를 받고 있어요 : 다음

구문 에러 : /Users/bbarton250/Sites/rails_projects/theoldman/app/models/comment.rb:11! 구문 오류, 예기치 못한 tANDOP, & & kEND 기대는 article.published? ^ /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in load' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in load_file ' /opt/local/lib/ruby ​​/ gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb : 639 : new_constants_in' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in load_file ' /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib /active_support/dependenciesencies.rb:353:in require_or_load' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in load_missing_constant ' from /opt/local/lib/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in each' /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:514:in load_missing_constant ' /opt/local/lib/ruby ​​/ gems/1.8/gems/activesup 포트 3.2.3/lib/active_support/dependencies.rb : 192 : const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in 각 ' /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb : 190 : in const_missing' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:218:in constantize ' /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:217:in에서 each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:217:in constantize' /opt에서 /local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:554:in get' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:in constantize ' /opt/local/lib/ruby/gems/1.8/gems/에서 가져옵니다. activerecord-3.2.3/lib/active_record/inheritance.rb : 111 : compute_type' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in에서 각각 ' /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb : 109 : compute_type' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in에 ' 보낸 사람 /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in klass' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:148:in 거래 ' /opt/local/lib/ruby/gems/1.8/에서 gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb : 431 : create_record' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:119:in에서 ' 을 작성하십시오. /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record

: 여기
class Comment < ActiveRecord::Base 
    attr_accessible :body, :email, :name, :article_id 

    belongs_to :article 

    validates :name, :email, :body, :presence => true 
    validates :article_should_be_published 

    def article_should_be_published 
    errors.add(:article_id, "is not published yet") if article 
&& !article.published? 
    end 
end 

내 문서 모델 : __send__' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in /associations/collection_proxy.rb:46:in 여기

내 의견 모델이다 '만들

iltempos 팁을 따른 후 ... 내가 :

class Article < ActiveRecord::Base 
    attr_accessible :body, :published_at, :title 

    validates :title, :presence => true 
    validates :body, :presence => true 

    belongs_to :user 
    has_and_belongs_to_many :categories 
    has_many :comments 

    scope :published, where("articles.published_at IS NOT NULL") 
    scope :draft, where("articles.published_at IS NULL") 
    # recent post from < a month ago - see pg 103 of tutorial 
    # scope :recent, lambda { published.where("articles.published_at > ?", 1.month.ago.to_date)} 
    scope :where_title, lambda { |term| where("articles.title LIKE ?", "%#{term}%") } 

    def long_title 
    "#{title} - #{published_at}" 
    end 

    def published? 
    published_at.present? 
    end 
end 

내가

UPDATE 너무 많이 일체의 도움을 ... 감사 감사 ... 난 무엇을 제공 할 필요가 있으면 알려 주시기 바랍니다 이제 다음과 같은 유효성 검사 오류가 발생했습니다 ...

하면 ArgumentError : 당신은 /opt/local/lib/ruby/gems/1.8/gems/activemodel-3.2.3/lib/active_model/validations/validates.rb:86:in에서 적어도 하나 개의 검증 를 제공해야 validates' from /Users/bbarton250/Sites/rails_projects/theoldman/app/models/comment.rb:7 from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in로드 ' /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in load_file' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:639:in new_constants_in' /opt/local/lib/ruby에서 가져 오기 /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/에서 /gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in load_file' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:in require_or_load ' lib 디렉토리/active_support/dependencies.rb : 502 : /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_suppo에서 load_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in const_missing ' 에서 rt/dependencies.rb : 190 : each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in에서 const_missing '/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:514:in에서 load_missing_constant' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in const_missing' /opt/local/lib/ruby/gems/1.8에서 /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in each' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in const_missing ' 에서 /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/에서 /gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:218:in constantize' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:217:in 각 ' active_support/inflector/methods.rb : 217 : constantize' from /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:554:in /opt/local/lib/ruby/gems/1.8/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:588:in에서 을 가져 오십시오. constantize' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:111:in compute_type '/opt/local/lib/ruby/gems/1.8/g의 EMS/액티브-3.2.3/LIB/active_record/inheritance.rb : 109 : each' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/inheritance.rb:109:in compute_type에서 ' /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection 행 .rb : 172 : send' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/reflection.rb:172:in에서 ' /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:148:in transaction' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:431:in create_record' 에서 /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_association.rb:119:in create' from /opt/local/lib/ruby/gems/1.8/gems/activerecord-3.2.3/lib/active_record/associations/collection_proxy.rb:46:in ' /opt/local/lib/ruby ​​/ 보석/1.8/보석/액티브-3.2.3/lib 디렉토리/active_record/협회/collection_proxy.rb : 46 :`에서 '

답변

1

모양을 만들 거기에는 줄 바꿈이 없어야합니다. :

def article_should_be_published 
    errors.add(:article_id, "is not published yet") if article && !article.published? 
    end 
+0

iltempo ... 감사합니다. 지금은 새로운 오류가 나타납니다. 나는 그것을 위에 포함시켰다. 이 "적어도 하나의 유효성 검사를 제공해야합니다"오류에 대한 아이디어. – BennyB

+0

Nevermind ilTempo ... "validates :" "validate :"를 사용해야했을 때 ... ... 도움을 청합니다. – BennyB

+0

나는 이것을 적어 두었습니다 :-) 당신은 환영합니다. – iltempo

관련 문제