Rails:跟随和跟随系统出现问题

发布于 2025-01-05 05:39:03 字数 2456 浏览 0 评论 0原文

这是我得到的错误

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌能量女王 2025-01-12 05:39:03

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

Shouldn't you just be using if current_user.following?(@user) instead of if current_user.followed_users?(@user) on line 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文