关注 Michael Hartl 的 Rails 教程中的功能
Following Feature From Michael Hartl's Rails Tutorial
我正在尝试实现 Hartl 的 RoR 教程第 12 章中的跟随者功能。
我有一个功能可以登录,转到另一个用户个人资料,单击关注,然后(刷新页面后)我看到我现在正在关注他们。与取消关注相同。
但是,我的集成测试失败了,Ajax 不起作用,当我单击关注或取消关注时,我的 Rails 服务器日志中出现此错误:
Rendered users/_follow.html.erb (1.7ms)
Rendered relationships/destroy.js.erb (2.5ms)
Completed 500 Internal Server Error in 17ms
NoMethodError - undefined method `id' for nil:NilClass:
我的代码与 Hartl 的完全匹配(我认为)。但是,我在我的用户模型和 Devise 上使用了 FriendlyID,其中之一(可能是 FriendlyID)导致了问题。
我相信这是所有相关代码。
如有任何想法,我们将不胜感激...现在是第 5 个小时左右!
controllers/users_controller.rb
def following
@title = "Following"
@user = User.friendly.find(params[:id])
@users = @user.following
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.friendly.find(params[:id])
@users = @user.followers
render 'show_follow'
end
controllers/relationships_controller.rb
def create
user = User.friendly.find(params[: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
models/user.rb
# follows a user
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end
# unfollows a user
def unfollow(other_user)
active_relationships.find_by(followed_id: other_user.id).destroy
end
# returns true if user is following the other user
def following?(other_user)
following.include?(other_user)
end
_unfollow.html.erb
<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id),
html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
_follow.html.erb
<%= form_for(current_user.active_relationships.build(followed_id: @user.id), remote: true) do |f| %>
<div><%= hidden_field_tag :followed_id, @user.id %></div>
<%= f.submit "Follow", class: "btn" %>
<% end %>
在您的 relationships_controller.rb
中您没有定义 @user
变量,这就是您收到 undefined method
错误的原因。试试这个:
def create
@user = User.friendly.find(params[: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
我正在尝试实现 Hartl 的 RoR 教程第 12 章中的跟随者功能。
我有一个功能可以登录,转到另一个用户个人资料,单击关注,然后(刷新页面后)我看到我现在正在关注他们。与取消关注相同。
但是,我的集成测试失败了,Ajax 不起作用,当我单击关注或取消关注时,我的 Rails 服务器日志中出现此错误:
Rendered users/_follow.html.erb (1.7ms)
Rendered relationships/destroy.js.erb (2.5ms)
Completed 500 Internal Server Error in 17ms
NoMethodError - undefined method `id' for nil:NilClass:
我的代码与 Hartl 的完全匹配(我认为)。但是,我在我的用户模型和 Devise 上使用了 FriendlyID,其中之一(可能是 FriendlyID)导致了问题。
我相信这是所有相关代码。
如有任何想法,我们将不胜感激...现在是第 5 个小时左右!
controllers/users_controller.rb
def following
@title = "Following"
@user = User.friendly.find(params[:id])
@users = @user.following
render 'show_follow'
end
def followers
@title = "Followers"
@user = User.friendly.find(params[:id])
@users = @user.followers
render 'show_follow'
end
controllers/relationships_controller.rb
def create
user = User.friendly.find(params[: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
models/user.rb
# follows a user
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end
# unfollows a user
def unfollow(other_user)
active_relationships.find_by(followed_id: other_user.id).destroy
end
# returns true if user is following the other user
def following?(other_user)
following.include?(other_user)
end
_unfollow.html.erb
<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id),
html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
_follow.html.erb
<%= form_for(current_user.active_relationships.build(followed_id: @user.id), remote: true) do |f| %>
<div><%= hidden_field_tag :followed_id, @user.id %></div>
<%= f.submit "Follow", class: "btn" %>
<% end %>
在您的 relationships_controller.rb
中您没有定义 @user
变量,这就是您收到 undefined method
错误的原因。试试这个:
def create
@user = User.friendly.find(params[: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