友谊状态检测 - Rails 5

时间:2017-08-18 00:48:19

标签: ruby-on-rails ruby

我正在使用Ruby on Rails创建社交网络,如果用户是,我想显示以下链接:

不是朋友展示"不是朋友" 是朋友展示"朋友" 待处理的请求显示"待处理"

但是我得到了一个"未初始化的常数所有者::友谊" friendship = Friendship.where(owner_id: [self.id,owner_2.id], friend_id: [self.id,owner_2.id])

上的错误

owner.rb

    has_many :follows, dependent: :destroy
  has_many :inverse_follows, class_name: "Follow", foreign_key: "friend_id", 
  dependent: :destroy

  def request_friendship(owner_2)
    self.friendships.create(friend: owners_2)
  end

  def pending_friend_requests_from
    self.inverse_friendships.where(state: "pending")
  end

   def pending_friend_requests_to
    self.friendships.where(state: "pending")
  end

  def active_friends
    self.friendships.where(state: "active").map(&:friend) + self.inverse_friendships.where(state: "active").map(&:owner)
  end

  def friendship_status(owner_2)
        friendship = Friendship.where(owner_id: [self.id,owner_2.id], friend_id: [self.id,owner_2.id])
        unless friendship.any?
          return "not_friends"
        else
          if friendship.first.state == "active"
            return "friends"
          else
            if friendship.first.owner == self
              return "pending"
            else
              return "requested"
            end
          end
        end
    end

end

owners_helper.rb

module OwnersHelper
    def action_buttons(owner)
        case current_owner.friendship_status(owner) when "friends"
            "Remove Friendship"
        when "pending"
            "Cancel Request"
        when "requested"
            "Accept or Deny"
        when "not_friends"
            "Add as a Friend"
        end
    end
end

_owner.html.erb

<h3>
<small><%= owner.username %></small><br>
<%= owner.fullname %>`</h3>

<p>
    <%= owner.businessname %>
</p>

<% if current_owner %>
    <%= action_buttons(owner)%>
<% end %>

follow.rb

    class Follow < ApplicationRecord
    belongs_to :owner
    belongs_to :friend, class_name: "Owner"
end

friendship_controller.er

    class FriendshipsController < ApplicationController
    before_action :authenticate_owner!
    before_action :set_owner, only: [:create]


    def create
        @friendship = current_owner.request_friendship(@owner)
        respond_to do |format|
            format.html {redirect_to owners_path, notice: "Friendship Created"}
        end
    end

    def destroy
        @friendship.destroy
        respond_to do |format|
            format.html {redirect_to owners_path, notice: "Friendship Deleted"}
        end
    end

    private

    def set_owner
        @owner = Owner.find(params[:owner_id])
    end
end

0 个答案:

没有答案