gpt4 book ai didi

ruby-on-rails - rails : Follow/Following Relationship Button

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

我有一个用户模型,可以创建关系以拥有关注者并关注其他人,我从 Rails 教程中学到的一切都非常有效。一件事是,我正在尝试通过添加能够看到其他人的关注者并能够拥有关注/关注按钮选项的选项来将其推进到下一步。显然,已经存在的代码将所有内容都放在了它应该在的地方(关注者小文件、按钮等),但是当我点击按钮时,它只会关注我自己或取消关注我自己,而不是我选择关注的特定人,如何我可以解决这个问题吗?非常感谢任何帮助!

这是以下和关注者页面的代码:

<div id='ContentContainer'>
<div class='ContentLeft'>
<% if @users.any? %>
<%= render @users %>
<%= will_paginate @users %>
<% end %>
</div>
</div>

这导致呈现包含此内容的用户模板

<% unless current_user?(@user) %>
<div id="follow_form_small">
<% if current_user.followed_users?(@user) %>
<%= render 'unfollow_small' %>
<% else %>
<%= render 'follow_small' %>
<% end %>
</div>
<% end %>

然后分别是关注和取消关注的表单

<%= form_for current_user.relationships.build(followed_id: @user.id),
remote: true do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", :class => 'FollowButton_small' %>
<% end %>

<%= form_for current_user.relationships.find_by_followed_id(@user),
html: { method: :delete },
remote: true do |f| %>
<%= f.submit "Unfollow", :class => 'UnFollowButton_small' %>
<% end %>

关系控制者

class RelationshipsController < ApplicationController

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
end

关系模型

class Relationship < ActiveRecord::Base
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
end

User Model 这个也加进去了

def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end

def followed_users?(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

遵循表格

<% unless current_user?(@user) %>
<div id="follow_form_small">
<% if current_user.followed_users?(@user) %>
<%= render 'unfollow_small' %>
<% else %>
<%= render 'follow_small' %>
<% end %>
</div>
<% end %>

当前用户

private

def current_user
@current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
end
helper_method :current_user

新编辑

class Relationship < ActiveRecord::Base
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

validate :not_following_himself

def not_following_himself
errors.add :followed_id,
if followed_id == follower_id
end

end

最佳答案

问题出在 current_user? 方法上。它可能总是返回 true 并且在用户列表中跟随按钮显示所有用户(包括您的条目),因此通过单击关注您可以关注自己。

关于ruby-on-rails - rails : Follow/Following Relationship Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9306900/

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