我正在浏览Treehouse的互助友谊视频。我遇到的问题是,当用户点击“接受友谊”按钮时,会显示一条Flash消息,说明他们现在是我的朋友。但是,它不会在用户的“朋友”页面上更新。它仍然显示用户有“请求友谊”并接受它。不知怎的,它没有更新。
它表明友谊始于我,但它不会为其他用户更新。它仍然表示此人有友谊请求。请参阅下面的代码。先感谢您。
我不确定这是否与 mutual_friendship.update_attribute(:state,'accepted')
有关User_Friendship Model
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
state_machine :state, initial: :pending do
after_transition on: :accept, do: [:send_acceptance_email, :accept_mutual_friendship!]
state :requested
event :accept do
transition any => :accepted
end
end
def self.request(user1, user2)
transaction do
friendship1 = create!(user: user1, friend: user2, state: 'pending')
friendship2 = create!(user: user2, friend: user1, state: 'requested')
friendship1.send_request_email
friendship1
end
end
def send_request_email
UserNotifier.friend_requested(id).deliver
end
def send_acceptance_email
UserNotifier.friend_request_accepted(id).deliver
end
def mutual_friendship
self.class.where({user_id: friend_id, friend_id: user_id}).first
end
def accept_mutual_friendship!
# Grab the mutual friendship and update the state without using the state machine, so as
# not to invoke callbacks.
mutual_friendship.update_attribute(:state, 'accepted')
end
end
UserFriendship Controller
class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!
def index
@user_Friendships = current_user.user_friendships.all
end
def accept
@user_friendship = current_user.user_friendships.find(params[:id])
if @user_friendship.accept_mutual_friendship!
flash[:success] = "You are now friends with #{@user_friendship.friend.name}"
redirect_to user_friendships_path
else
flash[:error] = "That friendship could not be accepted"
redirect_to user_friendships_path
end
end
def new
if params[:friend_id]
@friend = User.find(params[:friend_id])
raise ActiveRecord::RecordNotFound if @friend.nil?
@user_friendship = current_user.user_friendships.new(friend: @friend)
else
flash[:error] = "Friend required"
end
rescue ActiveRecord::RecordNotFound
render file: 'public/404', status: :not_found
end
def create
if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
@friend = User.find(params[:user_friendship][:friend_id])
@user_friendship = UserFriendship.request(current_user, @friend)
if @user_friendship.new_record?
flash[:error] = "There was a problem creating this friend request."
else
flash[:success] = "Friend request sent."
end
redirect_to user_path(@friend)
else
flash[:error] = "Friend required"
redirect_to root_path
end
end
def edit
@user_friendship = current_user.user_friendships.find(params[:id])
@friend = @user_friendship.friend
end
def user_friendship
params.require(:user_friendship).permit(:user_id, :friend_id, :user, :friend, :state, :user_friendship)
end
end
用户/索引
<div class="page-header">
<h1> Friends </h1>
</div>
<% @user_Friendships.each do |friendship| %>
<% friend = friendship.friend %>
<div id="<%= dom_id(friendship) %>" class="friend row">
<div class="span1">
</div>
<div class="span7">
<strong><%= friend.name %></strong><br />
<%if friendship.pending? %>
<em>Friendship is pending.</em> <%=link_to "Delete request", edit_user_friendship_path(friendship) %>.
<% end %>
<% if friendship.requested? %>
<em>Friendship requested.</em> <%=link_to "Accept Friendship", edit_user_friendship_path(friendship) %>.
<% end %>
<% if friendship.accepted? %>
<em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(friendship) %>.
<% end %>
</div>
</div>
<% end %>
答案 0 :(得分:0)
是的,确实,它与
有关mutual_friendship.update_attribute(:state, 'accepted')
当您执行
时,您将在UserFriendship表中创建两个条目 friendship1 = create!(user: user1, friend: user2, state: 'pending')
friendship2 = create!(user: user2, friend: user1, state: 'requested')
但您只是在接受请求时更新其中一个。
我在控制器中的accept方法中添加了一行来解决问题...尝试一下,让我知道它是否有效
def accept
@user_friendship = current_user.user_friendships.find(params[:id])
@user_friendship.accept_mutual_friendship!
@user_friendship.friend.user_friendships.find_by(friend_id: current_user.id).accept_mutual_friendship!
flash[:success] = "You are now friends with #{@user_friendship.friend.name}"
redirect_to user_friendships_path
end