Rails:跟随和跟随系统出现问题
这是我得到的错误
undefined method `followed_users?' for #<User:0x007fdadbf11e28>
提取的源代码(围绕第 #3 行):
1: <% unless current_user?(@user) %>
2: <div id="follow_form">
3: <% if current_user.followed_users?(@user) %>
4: <%= render 'unfollow' %>
5: <% else %>
6: <%= render 'follow' %>
如果我删除问号,我会得到此错误
undefined method `model_name' for NilClass:Class
提取的源代码(围绕第 #1 行):
1: <%= form_for current_user.relationships.find_by_followed_id(@user),
2: html: { method: :delete },
3: remote: true do |f| %>
4: <div class="actions"><%= f.submit "Unfollow" %></div>
我非常困惑,这里是我的
用户模型< 的代码/strong>
has_many :microposts
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
关系模型
attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
用户控制器
def show
@user = User.find(params[:id])
@micropost = Micropost.new
@microposts = @user.microposts.paginate(page: params[:page])
end
关系控制器
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
This is the error I get
undefined method `followed_users?' for #<User:0x007fdadbf11e28>
Extracted source (around line #3):
1: <% unless current_user?(@user) %>
2: <div id="follow_form">
3: <% if current_user.followed_users?(@user) %>
4: <%= render 'unfollow' %>
5: <% else %>
6: <%= render 'follow' %>
If I remove the question mark I get this error
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: <%= form_for current_user.relationships.find_by_followed_id(@user),
2: html: { method: :delete },
3: remote: true do |f| %>
4: <div class="actions"><%= f.submit "Unfollow" %></div>
I am extremely confused here are my code for the
user model
has_many :microposts
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
relationship model
attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
user controller
def show
@user = User.find(params[:id])
@micropost = Micropost.new
@microposts = @user.microposts.paginate(page: params[:page])
end
relationship controller
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道你不应该在第 3 行使用
if current_user.following?(@user)
而不是if current_user.followed_users?(@user)
吗?Shouldn't you just be using
if current_user.following?(@user)
instead ofif current_user.followed_users?(@user)
on line 3.