1. rails g controller user index show
in usercontroller copy and paste
def index
@users = User.all
end
def show
@user= User.find(params[:id])
end
In user model copy and paste that code
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
mount_uploader :image, AvatarUploader
# Follows a user.
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end
# Unfollows a user.
def unfollow(other_user)
active_relationships.find_by(followed_id: other_user.id).destroy
end
# Returns true if the current user is following the other user.
def following?(other_user)
following.include?(other_user)
end
1. view/users/index.html
<p id="notice"><%= notice %></p>
<h1>users</h1>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<% @users.each do |x| %>
<a href="/users/<%= x.id %>">
<tr>
<td> <%= image_tag x.image, :size => "64x64" %></td>
<td> <a href="/users/<%= x.id %>"><%= x.email.split('@')[0] %></a></td>
<td><%= x.email %></td>
<td><div class="panel-body text-center">
<div id="follow_form_<%= x.id %>">
<% if current_user %>
<% if current_user.following?(x) %>
<%= render 'users/unfollow', x: x %>
<% else %>
<%= render 'users/follow', x: x %>
<% end %>
<%else %>
<%= render 'users/follow', x: x %>
<% end %>
</div>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<br>
2. view/users/show.html
<p id="notice"><%= notice %></p>
<div class=" row col-md-12">
<div class="pull-left">
<%= image_tag @user.image, :size => "64x64" %>
<p>
<strong>Name:</strong>
<%= @user.email.split('@')[0] %>
</p>
</div>
</div>
<div class="pull-right col-md-9">
<% if params[:user].to_s == "follower" %>
<center><h1>All Follower User</h1></center>
<% if current_user.followers.present? %>
<% current_user.followers.each_with_index do |follower, index| %>
<div class="row">
<div class="col-md-2"><%= image_tag follower.image, style: "height:50px; width:50px;", title: "#{follower.email}"%></div>
<div class="col-md-6" style="text-align: left;"><b><%= follower.email.try(:titleize) %></b></div>
<div class="col-md-4">
<div class="panel-body text-center">
<div id="follow_form_<%= follower.id %>">
<% if current_user %>
<% if current_user.following?(follower) %>
<% if user_signed_in? && follower!= current_user %>
<%= link_to "UnFollow", unfollow_path(:id => follower.try(:id), flw: false, parent_id: current_user.id, btnID: "followerBtn"), class: "btn btn-danger btn-lg btn-block", :method => :post, :remote => 'true'%>
<% end %>
<% else %>
<% if user_signed_in? && follower!= current_user %>
<%= link_to "Follow", relationships_path(:id => follower.try(:id), flw: false, parent_id: current_user.id, btnID: "followerBtn"), class: "btn btn-success btn-lg btn-block", :method => :post, :remote => 'true' %>
<% end %>
<% end %>
<% end %>
<b>Followers (<%= follower.followers.count %>) </b>
<b>Followings (<%= follower.following.count %>) </b>
</div>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
<%elsif params[:user].to_s == "following" %>
<center><h1>All Following User</h1></center>
<div class="modal-body">
<% if current_user.following.present? %>
<% current_user.following.each_with_index do |following, index| %>
<div class="row">
<div class="col-md-2"><%= image_tag following.image, style: "height:50px; width:50px;", title: "#{following.email}"%></div>
<div class="col-md-6" style="text-align: left;"><b><%= following.email.try(:titleize) %></b></div>
<div class="col-md-4">
<div class="panel-body text-center">
<div class="panel-body text-center">
<div id="follow_form_<%= following.id %>">
<% if current_user %>
<% if current_user.following?(following) %>
<% if user_signed_in? && following!= current_user %>
<%= link_to "UnFollow", unfollow_path(:id => following.try(:id), flw: false, parent_id: current_user.id, btnID: "followerBtn"), class: "btn btn-danger btn-lg btn-block", :method => :post, :remote => 'true'%>
<% end %>
<% else %>
<% if user_signed_in? && following!= current_user %>
<%= link_to "Follow", relationships_path(:id => following.try(:id), flw: false, parent_id: current_user.id, btnID: "followerBtn"), class: "btn btn-success btn-lg btn-block", :method => :post, :remote => 'true' %>
<% end %>
<% end %>
<% end %>
<b>Followers (<%= following.followers.count %>) </b>
<b>Followings (<%= following.following.count %>) </b>
</div>
</div>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
<%end%>
</div>
3. view/users/_follow.html.erb
<% if user_signed_in? && x!= current_user %>
<% if !(params[:flw] == "false") %>
<%= link_to "Follow", relationships_path(:id => x.try(:id)), class: "btn btn-success btn-lg btn-block", :method => :post, :remote => 'true' %>
<% else %>
<%= link_to "Follow", relationships_path(:id => x.try(:id), flw: false), class: "btn btn-success btn-lg btn-block", :method => :post, :remote => 'true' %>
<% end %>
<% end %>
<br>
<%= render partial: "users/followers_following", locals: {x: x, params: params} %>
4. view/users/_unfollow.html.erb
<% if user_signed_in? && x!= current_user %>
<% if !(params[:flw] == "false") %>
<%= link_to "UnFollow", unfollow_path(:id => x.try(:id)), class: "btn btn-danger btn-lg btn-block", :method => :post, :remote => 'true'%>
<% else %>
<%= link_to "UnFollow", unfollow_path(:id => x.try(:id), flw: false), class: "btn btn-danger btn-lg btn-block", :method => :post, :remote => 'true'%>
<% end %>
<% end %>
<br>
<%= render partial: "users/followers_following", locals: {x: x, params: params} %>
5. view/users/_followers_following.html.erb
<% if x.present? %>
<% if x.id == current_user.id %>
<b><a href="/users/<%= current_user.id %>?user=follower">Followers (<%= x.followers.count %>)</a></b>
<b><a href="/users/<%= current_user.id %>?user=following">Followings (<%= x.following.count %>) </a></b>
<%else%>
<b>Followers (<%= x.followers.count %>)</b>
<b>Followings (<%= x.following.count %>) </b>
<%end%>
<% end %>
2. rails generate model Relationship follower_id:integer followed_id:integer
after that go to see migration
class CreateRelationships < ActiveRecord::Migration[5.0]
def change
create_table :relationships do |t|
t.integer :follower_id
t.integer :followed_id
t.timestamps
end
add_index :relationships, :follower_id
add_index :relationships, :followed_id
add_index :relationships, [:follower_id, :followed_id], unique: true
end
end
rails db:migrate
3. rails g controller Relationships create unfollow
class RelationshipsController < ApplicationController
before_action :authenticate_user!
def create
@user = User.find(params[:id])
@resourceRel = current_user.follow(@user)
# if !(@resourceRel.errors.any?)
# Notification.notification(current_user, nil, "follow", @user)
# end
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def unfollow
@user = User.find(params[:id])
@resourceRel = current_user.unfollow(@user)
# if !(@resourceRel.errors.any?)
# Notification.notification(current_user, nil, "unfollow", @user)
# end
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
In Relationship model
class Relationship < ApplicationRecord
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
validate :ambiguity
def ambiguity
if self.follower_id == self.followed_id
errors.add(:_, "you can not followed your self")
end
if Relationship.all.collect(&:follower_id).include?(self.follower_id) && Relationship.all.collect(&:followed_id).include?(self.followed_id)
errors.add(:_, "ambiguity record can not create")
end
end
end
view/relationships/create.js.erb
<% if @resourceRel.errors.any? %>
<% @resourceRel.errors.each_with_index do |error, index| %>
alertify.message('<%= error[1] %>');
<% end %>
<% else %>
<% if params[:flw] == "false" %>
<% if params[:id].present? %>
<% @user = User.find(params[:id].to_i) %>
<%else%>
<% @parent_user = User.find(params[:parent_id].to_i) %>
<%end%>
// $('.modal-backdrop').removeClass('modal-backdrop');
$('#follow_form_<%= params[:parent_id].to_i %>').html('<%= escape_javascript(render partial: "users/unfollow",locals: { x: @parent_user, params: params }) %>')
$('#<%#= params[:btnID] %>').click();
$('#follow_form_<%= @user.id %>').html('<%= escape_javascript(render partial: "users/unfollow",locals: { x: @user, params: params }) %>')
<% else %>
$('#follow_form_<%= @user.id %>').html('<%= escape_javascript(render partial: "users/unfollow",locals: { x: @user, params: params }) %>')
<% end %>
<% end %>
view/relationships/unfollow.js.erb
<% if @resourceRel.errors.any? %>
<% @resourceRel.errors.each_with_index do |error, index| %>
alertify.message('<%= error[1] %>');
<% end %>
<% else %>
<% if params[:flw] == "false" %>
<% if params[:id].present? %>
<% @user = User.find(params[:id].to_i) %>
<%else%>
<% @parent_user = User.find(params[:parent_id].to_i) %>
<%end%>
// $('.modal-backdrop').removeClass('fade in');
$('#follow_form_<%= params[:parent_id].to_i %>').html('<%= escape_javascript(render partial: "users/follow", locals: {x: @parent_user, params: params }) %>')
$('#<%= params[:btnID] %>').click();
$('#follow_form_<%= @user.id %>').html('<%= escape_javascript(render partial: "users/follow", locals: {x: @user, params: params }) %>')
<% else %>
$('#follow_form_<%= @user.id %>').html('<%= escape_javascript(render partial: "users/follow", locals: {x: @user, params: params }) %>')
<% end %>
<% end %>
go to route.rb
resources :relationships, :only => [:create]
post 'unfollow' => 'relationships#unfollow', as: :unfollow
get '/users', to: "users#index"
get '/users/:id', to: "users#show"
No comments:
Post a Comment