質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

0回答

2967閲覧

友達申請機能を実装したい

NaojirouHisada

総合スコア60

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

1グッド

3クリップ

投稿2016/02/03 09:31

現在、Facebookのような友達申請機能を作成したいと考えております。
ツイッターのようなフォロー、フォロワーのような関係は作成できたのですが、
友達になる(申請) => 申請待ち => 承認(相手) =>友達関係
という手順が上手く作成できません。

お手数おかけしますが、
アドバイス宜しくお願いします。

私的には、
このように申請する側とされる側の外部キーと申請状態を表すstatusを持ったRlationshipというテーブルを作成しました。

class CreateRelationships < ActiveRecord::Migration def change create_table :relationships do |t| t.references :follow, index: true t.references :followed, index: true t.string :status t.timestamps null: false t.index [:follow_id , :followed_id] , unique: true end end end

そして、
両方の外部キーにはユーザのidが入るので、
Relationshipモデルを

class Relationship < ActiveRecord::Base belongs_to :follower, class_name: "User" belongs_to :followed, class_name: "User" end

ユーザモデルはこうです。

class User < ActiveRecord::Base has_one :member has_many :microposts has_many :comments has_many :following_relationships, class_name: "Relationship" , foreign_key: "follow_id" , dependent: :destroy has_many :following_users , through: :following_relationships , source: :followed has_many :follower_relationships, class_name: "Relationship" , foreign_key: "followed_id",dependent: :destroy has_many :follower_users, through: :follower_relationships , source: :follow has_secure_password accepts_nested_attributes_for :member # 他のユーザーをフォローする def follow(other_user ,status) following_relationships.create(followed_id: other_user.id , status: status.status) end # フォローしているユーザーをアンフォローする def unfollow(other_user) following_relationships.find_by(followed_id: other_user.id).destroy end # あるユーザーをフォローしているかどうか? def following?(other_user) following_users.include?(other_user) end end

そして、
relationshipsコントローラで、

class RelationshipsController < ApplicationController before_action :logged_in_user def create @user = User.find(params[:followed_id]) @status = Relationship.new(relationship_params) current_user.follow(@user , @status) end def destroy @user = current_user.following_relationships.find(params[:id]).followed current_user.unfollow(@user) end def edit end private def relationship_params params.require(:relationship).permit(:status) end end

友達になるがこうです

<%= form_for(current_user.following_relationships.build, remote: true) do |f| %> <div><%= hidden_field_tag :followed_id, @user.id %></div> <%= f.submit "Follow", class: "btn btn-primary" %> <% end %>

友達解除がこうです。

<%= form_for(current_user.following_relationships.find_by(followed_id: @user.id), html: { method: :delete }, remote: true) do |f| %> <%= f.submit "Unfollow", class: "btn" %> <% end %>

友達申請等はAjaxを使用しているため、
友達、解除のAjaxの記述はこうです。

create $("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>"); $("#followers").html('<%= @user.follower_users.count %>'); destroy $("#follow_form").html("<%= escape_javascript(render('users/follow')) %>"); $("#followers").html('<%= @user.follower_users.count %>');
act823👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問