Wednesday, 3 July 2019

Like/Dislike/Reply


How to clone project from git hub repository

2. bundle install
3. rake db:create
4. rake db:migrate

How to create a Like/Dislike in Blog App

1. create a div in post show form(posts/show.html.erb) and add..

   <div id="post_likes">
      <%= render "likes/likes", likeable: @post %>
</div>
2. create a view page of likes in likes folder(likes/_likes.html.erb) and add.

<% if likeable.already_liked?(current_user) %>
<%= link_to 'Unlike', like_path(likeable.likes.where(user_id: current_user.id).first), method: :delete, remote: true %>
<% else %>
<%= link_to 'Like', likes_path(likeable_type: likeable.class.name, likeable_id: likeable.id), method: :post, remote: true %>
<% end %>
<small><%= likeable.likes.count %> likes</small>
3. Next, create a js file in likes folder(likes/create.js.erb) and add..

<% if @like.likeable.class.name == "Post" %>
$("#post_likes").html("<%= escape_javascript render 'likes/likes', likeable: @like.likeable %>");
<% elsif @like.likeable.class.name == "Comment" %>
$("#comment_<%= @like.likeable.id %>_likes").html("<%= escape_javascript render 'likes/likes', likeable: @like.likeable %>");
<% else %>
$("#reply_<%= @like.likeable.id %>_likes").html("<%= escape_javascript render 'likes/likes', likeable: @like.likeable %>");
<% end %>
4. Next, create a js file in likes folder(likes/destroy.js.erb) and add..
<% if @like.likeable.class.name == "Post" %>
$("#post_likes").html("<%= escape_javascript render 'likes/likes', likeable: @like.likeable %>");
<% elsif @like.likeable.class.name == "Comment" %>
$("#comment_<%= @like.likeable.id %>_likes").html("<%= escape_javascript render 'likes/likes', likeable: @like.likeable %>");
<% else %>
$("#reply_<%= @like.likeable.id %>_likes").html("<%= escape_javascript render 'likes/likes', likeable: @like.likeable %>");
<% end %>
5. Next, create a controller likes_controller.rb

class LikesController < ApplicationController
# before_action :find_post
before_action :find_like, only:[:destroy]
def create
if current_user.likes.where(likeable_id: params[:likeable_id], likeable_type: params[:likeable_type]).exists?
flash[:notice]= "you can't like more than once"
else
@like = current_user.likes.create(likeable_id: params[:likeable_id], likeable_type: params[:likeable_type])
end
end

def destroy
@like.destroy
end

def find_like
@like = Like.find(params[:id])
end

# private
# def find_post
# @post = Post.find(params[:post_id])
# end
end

6. Next, relationship establish in comment.rb model and add ..
has_many :likes, as: :likeable, dependent: :destroy
def already_liked?(user)
likes.where(user_id: user.id).exists?
End
7. Next, like.rb model and add..

class Like < ApplicationRecord
belongs_to :likeable, polymorphic: true
belongs_to :user
end
8. Next, post.rb model and add..
class Post < ApplicationRecord
has_many :comments, dependent: :destroy
has_many :likes, as: :likeable, dependent: :destroy
belongs_to :user
validates :title, presence: true, length: {minimum: 5}

def already_liked?(user)
likes.where(user_id: user.id).exists?
end
end

How to create a reply on comment
1. render a reply form in comment in comments folder  (comments/comment.html.erb)

<div id="replyForm">
<%= render 'replies/form', comment: comment %>
</div>
2. render a showReply page in comments folder (comments/comment.html.erb)

<div id="replyContainer">
<%= render 'comments/showreply', comment: comment %>
</div>
3. View of replies form (replies/_form.html.erb)

<%= form_with(model:[comment.post, comment, comment.replies.build], remote:true) do |f|%>

<%= f.hidden_field :user_id, value: current_user.id %>
<div class="form-group row d-flex justify-content-center">
<div class="col-md-10">
<%= f.text_field :body, class: "form-control", placeholder: "Reply" %>
</div>
<div class="col-md-2">
<%= f.submit "Send", class: "btn btn-primary" %>
</div>
</div>
<% end %>

4. View of showReply Page (comments/_showReply.html.erb)
<% comment.replies.each do |r| %>
<h5 class="card-title"><%= r.body %></h5>
<p class="card-text"><%= r.user.name %></p>
<p class="card-text"><small>
<%= time_ago_in_words(r.created_at) %></small></p>

<span id="reply_<%= r.id %>_likes">
<%= render "likes/likes", likeable: r %>
</span>

<% if current_user == r.user %>
<span> | <%= link_to 'Delete',[r.comment.post, r.comment, r], method: :delete, data: {confirm: 'Are you sure?'} %></span>
<% end %>
<hr>
<% end %>
5. create a js file in replies folder (replies.create.js.erb) and add..
$("#replyConatainer").html("<%= escape_javascript render 'comments/showreply', comment: @comment. %>");
$("#replyForm").html("<%= escape_javascript render 'replies/form', comment: @comment %>");
6. Modify comment.html.erb in comments folder and add..
<% if current_user == comment.user || current_user == comment.post.user %>
<span> | <%= link_to 'Delete',[comment.post, comment], method: :delete, data: {confirm: 'Are you sure?'} %>
</span>
<% end %>

7. Modify showReply.html.erb in comments folder and add..
<% if current_user == r.user || current_user == r.comment.user %>
<span> | <%= link_to 'Delete',[r.comment.post, r.comment, r], method: :delete, data: {confirm: 'Are you sure?'} %></span>
<% end %>

No comments:

Post a Comment

  Common Git Commands   Take clone of a repository git clone <remote-repository-url> ex: git clone  https://github.com/agricor/RegTech...