- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个用户模型,可以创建关系以拥有关注者并关注其他人,我从 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/
我有这样的结构: class User(DeclarativeBase): ... teamMemberships = orm.relationship("TeamXREF",backre
我需要提取值 11,12 和 1,即 = 之后的值。 我正在尝试执行以下操作 Matcher m = Pattern.compile("\\((.*?)\\)").matcher(s); while
有三个表/模型: 客户: id - 客户端 品牌: id - 品牌 品牌型号: id - 型号 - Brand_id 品牌模型客户: id - Brandmodel_id - client_id 我希
在我的应用程序核心数据模型中,我有工作表和文本实体。工作表实体可以有两个文本:privacyNotes 和 termsOfUse。 都是文本类型。因此,在 XCode 数据建模器中,我在带有文本目标的
在现有的网络应用程序 (PHP/MySQL) 上工作,我得出了这一点:我有 2 个表存储名称和类型 ID。它们之间的关系是 NN 我之间有另一个表。 (见图) 我有一个名为“category”的最后一
当我创建一对一关系迁移时,laravel 创建一对多关系。 PHP 7.1 和 MySQL 5.7 模型是:角色和用户。 角色: public function user() { return
我有一个巨大的痛苦的继承表,其中包含几百万条记录,我无法有效地查询。表格看起来有点像这样: id | submissionId | key | value ------------------
我有两个实体,相册和照片,它们之间存在多对多关系。一切正常。我想要的是添加一个关系属性,即除了 album_id 和 photo_id 之外的映射的额外属性,例如添加照片的日期时间或表明照片在该相册中
过去几天我一直在努力解决我的问题,遗憾的是没有任何结果。我已经在这里阅读了无数关于这个主题的帖子,但我一直遇到同样的错误。 “'字段列表'中的未知列'Extent1.foo_id'”......我做错
我正在尝试映射两个实体之间的多对多关系,但我需要使用多个属性来装饰该实体 - 请参见下图: Reads 是我在这种情况下的关系表 - 我在上面添加了一个标识列以避免使用复合键,但这里有值(value)
我正在使用 Spring Data/Neo4j 和 REST API 开发一个 Web 应用程序。 我想知道我是否可以创建一种关系并以另一种关系结束,如下所示: @RelationshipEntity
您好,我在进行 Laravel 查询时遇到问题 模型区域 class Region extends Model { protected $table = 'regions'; prot
考虑以下场景: 我的 Laravel 应用程序中有几个实体,如下所示: 发布 页面 图片 视频 以上所有实体都可以有CustomFieldValue,这是另一个实体。 custom_field_val
乍一看,它们似乎在做同样的事情:定义一对多关系 那么你为什么要选择一个或另一个? 最佳答案 EmbedsMany - 嵌入另一个模型的许多实例的模型;例如,一个客户可以有多个电子邮件地址,每个电子邮件
我是 Neo4j 的新手,只是为了深入学习。我有一个小小的疑问,例如, 如果我在空间中创建了两个节点并提供了这两个节点之间的关系以及系统时间(知道它们在什么时间成为 friend )。 所以现在我的问
我想知道是否有人可以帮助我。 这就是我需要做的:将语言表的 ID 拉入新表中,并使其之间具有唯一的关系。就像节点的公式 n(n-1) 一样。 我尝试使用笛卡尔积,但它不起作用,因为列 a = 1 和列
如何识别关系是三元关系还是聚合关系? 例如:一名球员在他参加的比赛中获得奖励。如果实体是玩家、比赛和奖励,这是三元还是聚合? 最佳答案 一个讲师给我的答案,它是一个三元关系,因为涉及到 3 个贡献实体
实体关系是否有标准(非图形)表示法? 现在我正在使用我自己的 janky 符号: 用户 >> 照片, (1-many) 用户 > 个人资料,(1-1 hasOne) 个人资料 标签,(多对多) 最佳
假设有两个实体,名为 Employee 和 Campaign。一名员工可以参与多个事件。一个事件可以有很多员工。我已经知道这是一个多对多的关系。 绘制ER图时(用陈符号),这种关系用菱形来表示。但是对
以下ER图中的双线是表示完全参与还是递归关系? 谁能告诉我两者的符号区别? 最佳答案 总参与人数:集合中的每个实体都涉及关系的某个关联(或元组)。 递归关系:与自身相关的实体集。 在递归关系中最常见的
我是一名优秀的程序员,十分优秀!