gpt4 book ai didi

ruby-on-rails - rails : Having problems with follow and followed system

转载 作者:数据小太阳 更新时间:2023-10-29 08:52:35 25 4
gpt4 key购买 nike

这是我得到的错误

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>

我非常困惑这里是我的代码

用户模型

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

用户 Controller

  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

最佳答案

你不应该在第 3 行使用 if current_user.following?(@user) 而不是 if current_user.followed_users?(@user)

关于ruby-on-rails - rails : Having problems with follow and followed system,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9271340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com