Ich habe eine Follower/Following-Beziehung implementiert, ich möchte die Funktionalität, d. In meiner aktuellen Implementierung folgt User 'A' Benutzer 'B' ohne die Bestätigung von Benutzer 'B'. Ich möchte Benutzer 'A' eine Anfrage an Benutzer 'B' senden und dann Benutzer 'B' entweder annimmt oder ablehnt möchte, dass es wie das Instagram-Modell und nicht das Facebook-Modell ist. [Benutzer A sendet eine Folgeanforderung an Benutzer B. Wenn Benutzer B eine Anfrage annimmt, folgt Benutzer A Benutzer B und Benutzer B folgt nicht Benutzer A, um dies zu tun, muß Benutzer B senden eine Anfrage an Benutzer A].Folgen Sie Anfrage Ruby on Rails
Meine Dateien:
schema.rb
class CreateFollowJoinTable < ActiveRecord::Migration
def change
create_table 'follows' do |t|
t.integer 'following_id', :null => false
t.integer 'follower_id', :null => false
t.boolean :accepted, default: false
t.timestamps null: false
end
add_index :follows, :following_id
add_index :follows, :follower_id
add_index :follows, [:following_id, :follower_id], unique: true
end
end
app/models/follow.rb
class Follow < ActiveRecord::Base
belongs_to :follower, foreign_key: 'follower_id', class_name: 'User'
belongs_to :following, foreign_key: 'following_id', class_name: 'User'
end
app/models/user.rb
has_many :follower_relationships, foreign_key: :following_id, class_name: 'Follow'
has_many :followers, through: :follower_relationships, source: :follower
has_many :following_relationships, foreign_key: :follower_id, class_name: 'Follow'
has_many :following, through: :following_relationships, source: :following
def follow(user_id)
following_relationships.create(following_id: user_id)
end
def unfollow(user_id)
following_relationships.find_by(following_id: user_id).destroy
end
Routen .rb
post ':user_name/follow_user', to: 'relationships#follow_user', as: :follow_user
post ':user_name/unfollow_user', to: 'relationships#unfollow_user', as: :unfollow_user
app/controllers/relationships_controller.rb
class RelationshipsController < ApplicationController
def follow_user
@user = User.find_by! user_name: params[:user_name]
if current_user.follow @user.id
respond_to do |format|
format.html { redirect_to root_path }
format.js
end
end
end
def unfollow_user
@user = User.find_by! user_name: params[:user_name]
if current_user.unfollow @user.id
respond_to do |format|
format.html { redirect_to root_path }
format.js
end
end
end
end
Well in Instagram, können Benutzer von beliebigen Nutzern ohne Anfragen folgen. – Pavan
Sie folgen mirael hartl's railstutorial.org richtig? – icemelt
nein @icemelt ich bin nicht, auch in Michael hartl Tutorial kann ein Benutzer ohne Aufforderung einer anderen folgen – Ashksta