我正在为关注用户制作一款新应用,但我遇到了一些麻烦。这是我的错误:
NameError in Welcome#sucess
Showing ../welcome/sucess.html.erb where line #13 raised:
uninitialized constant User::FollowingRelation
Extracted source (around line #13):
10: <br />
11: Following
12: <ul>
13: <% @user.following.each do |u| %>
14: <li><%= link_to u.username, u %></li>
15: <% end %>
16: </ul>
我可以在我的代码中找到User :: FollowingRelation:
*我的用户模型
class User < ActiveRecord::Base
attr_accessible :pass, :username
# Who am I following?
has_many :following_relations, :foreign_key => :follower_id
has_many :following, :through => :following_relations
# Who am I followed by?
has_many :follower_relations, :class_name => "Relation", :foreign_key => :following_id
has_many :followers, :through => :follower_relations
validates :username, :pass, :presence => true
validates :username, :pass, :length => { :minimum => 4 }
validates :username, :uniqueness => true
def self.login(username,pass)
user = find_by_username(username) and user = find_by_pass(pass)
if user.nil?
return nil
else
return user
end
end
end
*相关模型
class Relation < ActiveRecord::Base
belongs_to :follower, :class_name => "User"
belongs_to :following, :class_name => "User"
end
*行动成功
def sucess
@user = User.find(params[:id])
@relation = Relation.new
end
我是Ruby on Rails的新手,所以请帮助我:)。
答案 0 :(得分:0)
您需要修改此行:
has_many :following_relations, :foreign_key => :follower_id
要:
has_many :following_relations, class_name: 'Relation', :foreign_key => :follower_id