使用devise_invitable,一经接受,保存相互友谊

Using devise_invitable, upon acceptance, saving mutual friendship

我希望邀请用户和被邀请用户在通过 devise_invitable (gem) 接受电子邮件邀请后成为共同的朋友。

所以数据库应该是这样的:

 id | user_id | friend_id |         created_at         |         updated_at         
----+---------+-----------+----------------------------+----------------------------
  2 |       3 |         2 | 2020-10-13 02:42:03.33376  | 2020-10-13 02:42:03.33376
  3 |       2 |         3 | 2020-10-13 15:57:48.359378 | 2020-10-13 15:57:48.359378

这是我从源代码继承的邀请控制器中的内容。中间从@inviting_user = User.find(current_user.invited_by_id)开始的代码是我插入的。

class Users::InvitationsController < Devise::InvitationsController
  

prepend_before_action :require_no_authentication, only: [:edit, :destroy]


    def update
      raw_invitation_token = update_resource_params[:invitation_token]
      self.resource = accept_resource
      invitation_accepted = resource.errors.empty?
  
      yield resource if block_given?
  
      if invitation_accepted
        if resource.class.allow_insecure_sign_in_after_accept
          flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
          set_flash_message :notice, flash_message if is_flashing_format?
          resource.after_database_authentication
          sign_in(resource_name, resource)


          ### from here is my code:
          @inviting_user = User.find(current_user.invited_by_id)
          @invited_user = User.find(current_user.id)

          @friendship = @invited_user.friendships.build(:friend_id => params[:friend_id])
          @inverse_friendship = @inviting_user.friendships.build(:friend_id => params[:friend_id])

          if @friendship.save and @inverse_friendship.save
            # flash[:notice] = "Added friend."
            # redirect_back fallback_location: new_user_invitation_path
          else
            # flash[:error] = "Unable to add friend."
            # redirect_back fallback_location: new_user_invitation_path
          end
          ### my code end

          respond_with resource, location: after_accept_path_for(resource)

        else
          set_flash_message :notice, :updated_not_active if is_flashing_format?
          respond_with resource, location: new_session_path(resource_name)
        end
      else
        resource.invitation_token = raw_invitation_token
        respond_with_navigational(resource) { render :edit }
      end
    end

end

问题是没有建立友谊。如果有任何建议,我将不胜感激。

源代码控制器:https://github.com/scambra/devise_invitable/blob/master/app/controllers/devise/invitations_controller.rb

我开始认为这两行是问题所在,因为@inviting_user 和@invited_user 似乎已设置。在 devise_invitable 的上下文中,您在这里看到任何问题了吗?

@friendship = @invited_user.friendships.build(:friend_id => params[:friend_id])
@inverse_friendship = @inviting_user.friendships.build(:friend_id => params[:friend_id])

当我将@friendship.save 切换为@friendship.create 时!我得到“你是说 create_user 吗?”

您真的不需要此代码的一小部分。几乎所有的 Devise 控制器都会让步,因此您可以通过传递一个块来简单地使用 super 方法:

class Users::InvitationsController < Devise::InvitationsController
  def update
    super do |user|
      Friendship.transaction do 
        begin
          user.friendships.create!(friend_id: user.invited_by_id)
          User.find(user.invited_by_id)
              .friendships.create!(friend_id: user.id)
        rescue ActiveRecord::RecordInvalid
          flash[:error] = "Unable to add friend."
          redirect_back fallback_location: new_user_invitation_path
          raise ActiveRecord::Rollback
        end
      end
    end
  end
end