第11章迈克尔哈特尔错误

时间:2014-07-24 10:33:36

标签: ruby-on-rails

我在Michael Hartl的书上做样本应用程序,在第11章中有一些错误

显示/home/andrey/RoR/sample_app/app/views/shared/_stats.html.erb第5行提出:

未知验证者:'PresenseValidator'

提取的来源(第5行):

5<%= @ user.followed_users.count%>

模板包含跟踪:app / views / static_pages / home.html.erb

Rails.root:/ home / andrey / RoR / sample_app 应用程序跟踪|框架跟踪|完整跟踪

app / models / relationship.rb:4:<class:Relationship>' app/models/relationship.rb:1:in' app / views / shared / _stats.html.erb:5:in _app_views_shared__stats_html_erb__765092261__621887978' app/views/static_pages/home.html.erb:8:in _ app_views_static_pages_home_html_erb__496074363__614039518'

user.rb

class User&lt;的ActiveRecord :: Base的   has_many:microposts,dependent :: destroy   has_many:relationships,foreign_key:“follower_id”,依赖:: destroy   has_many:followed_users,through :: relationship,source ::: follow   has_many:reverse_relationships,foreign_key:“followed_id”,class_name:“Relationships”,依赖:: destroy   has_many:关注者,通过:: reverse_relationships,source :: follower

before_save {self.email = email.downcase}   before_create:create_remember_token

验证:name,presence:true,length:{maximum:50}

VALID_EMAIL_REGEX = / \ A [\ w + - 。] + @ [a-z \ d - ] +(。[a-z] +)*。[a-z] + \ z / i

验证:电子邮件,在线状态:是,格式:{with:VALID_EMAIL_REGEX},唯一性:{case_sensitive:false}

has_secure_password

验证:密码,长度:{minimum:6}

def User.new_remember_token     SecureRandom.urlsafe_base64   端

def User.encrypt(令牌)     摘要:: SHA1.hexdigest(token.to_s)   端

def feed     Micropost.where(“user_id =?”,id)   端

def def?(other_user)     relationships.find_by(followed_id:other_user.id)   端

def def!(other_user)     relationships.create!(follow_id:other_user.id)   端

def deffollow!(other_user)     relationships.find_by(followed_id:other_user.id).destroy!   端

私有

def create_remember_token
  self.remember_token = User.encrypt(User.new_remember_token)
end

stats.erb.html

 <% @user ||= current_user %>
 <div class="stats">
 <a href="<%= following_user_path(@user) %>">
 <strong id="following" class="stat">
  <%= @user.followed_users.count %>
 </strong>
 following
 </a>
 <a href="<%= followers_user_path(@user) %>">
 <strong id="followers" class="stat">
  <%= @user.followers.count %>
 </strong>
 followers
 </a>
 </div>

relationship.rb

类关系&lt;的ActiveRecord :: Base的   belongs_to:follower,class_name:“User”   belongs_to:follow,class_name:“User”   验证:follower_id,presense:true   验证:follows_id,presense:true 端

1 个答案:

答案 0 :(得分:5)

你的关系中有拼写错误.rb

validates :follower_id, presense: true 
validates :followed_id, presense: true

应该是

validates :follower_id, presence: true
validates :followed_id, presence: true