Skip to main content
added 15 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

LikesControler for a Post - Rails

I have this controller that I call via Ajax, where I update the likes of a post.

I have two methods in the user model.

already_like_post?: Where I pass the posted ID and return true or false if the post is already liked.

dislike_post: Delete the user like for given post.

  • already_like_post?: Where I pass the posted ID and return true or false if the post is already liked.
  • dislike_post: Delete the user like for given post.
class LikesController < ApplicationController
    def create
        if !current_user.already_liked_post? params[:post_id]
            like = Like. new user: current_user, post_id: params[:post_id]
            if !like.save
                render json: like.errors
            end
        else
           current_user.dislike_post params[:post_id]
        end
        amount_of_likes = Post.find(params[:post_id]).likes.count
        render json:{status:"success",likes: amount_of_likes}
    end
end

#TheseThese are the methods in the user model:

def already_liked_post?(post_id)
    self.likes.where(post_id: post_id).size == 1
end

def dislike_post(post_id)
    self.likes.find_by(post_id: post_id).destroy
end

Can you give a review of this? I don't know if this looks good or if it is the Rails way.

LikesControler for a Post - Rails

I have this controller that I call via Ajax, where I update the likes of a post.

I have two methods in the user model.

already_like_post?: Where I pass the posted ID and return true or false if the post is already liked.

dislike_post: Delete the user like for given post.

class LikesController < ApplicationController
    def create
        if !current_user.already_liked_post? params[:post_id]
            like = Like. new user: current_user, post_id: params[:post_id]
            if !like.save
                render json: like.errors
            end
        else
           current_user.dislike_post params[:post_id]
        end
        amount_of_likes = Post.find(params[:post_id]).likes.count
        render json:{status:"success",likes: amount_of_likes}
    end
end

#These are the methods in the user model:

def already_liked_post?(post_id)
    self.likes.where(post_id: post_id).size == 1
end

def dislike_post(post_id)
    self.likes.find_by(post_id: post_id).destroy
end

Can you give a review of this? I don't know if this looks good or if it is the Rails way.

LikesControler for a Post

I have this controller that I call via Ajax, where I update the likes of a post.

I have two methods in the user model:

  • already_like_post?: Where I pass the posted ID and return true or false if the post is already liked.
  • dislike_post: Delete the user like for given post.
class LikesController < ApplicationController
    def create
        if !current_user.already_liked_post? params[:post_id]
            like = Like. new user: current_user, post_id: params[:post_id]
            if !like.save
                render json: like.errors
            end
        else
           current_user.dislike_post params[:post_id]
        end
        amount_of_likes = Post.find(params[:post_id]).likes.count
        render json:{status:"success",likes: amount_of_likes}
    end
end

These are the methods in the user model:

def already_liked_post?(post_id)
    self.likes.where(post_id: post_id).size == 1
end

def dislike_post(post_id)
    self.likes.find_by(post_id: post_id).destroy
end

Can you give a review of this? I don't know if this looks good or if it is the Rails way.

added 25 characters in body
Source Link
user34073
user34073

I have this controller that iI call via ajaxAjax, where iI update the likes of a post.

I have two methods in the user model.

already_like_post?already_like_post?: where iWhere I pass the post idposted ID and return true or false if hethe post is already likeliked.

dislike_postdislike_post: deleteDelete the user like for given post.

class LikesController < ApplicationController
    def create
        if !current_user.already_liked_post? params[:post_id]
            like = Like. new user: current_user, post_id: params[:post_id]
            if !like.save
                render json: like.errors
            end
        else
           current_user.dislike_post params[:post_id]
        end
        amount_of_likes = Post.find(params[:post_id]).likes.count
        render json:{status:"success",likes: amount_of_likes}
    end
end

#This#These are the methods in the user model def already_liked_post?(post_id) self.likes.where(post_id: post_id).size == 1 end

def already_liked_post?(post_id)
    self.likes.where(post_id: post_id).size == 1
end

def dislike_post(post_id)
    self.likes.find_by(post_id: post_id).destroy
end

Can you give a review of this? I dontdon't know if this looks good and itsor if it is the railsRails way.

I have this controller that i call via ajax, where i update the likes of a post.

I have two methods in user model

already_like_post?: where i pass the post id and return true or false if he already like.

dislike_post: delete the user like for given post.

class LikesController < ApplicationController
    def create
        if !current_user.already_liked_post? params[:post_id]
            like = Like. new user: current_user, post_id: params[:post_id]
            if !like.save
                render json: like.errors
            end
        else
           current_user.dislike_post params[:post_id]
        end
        amount_of_likes = Post.find(params[:post_id]).likes.count
        render json:{status:"success",likes: amount_of_likes}
    end
end

#This are the methods in the user model def already_liked_post?(post_id) self.likes.where(post_id: post_id).size == 1 end

def dislike_post(post_id)
    self.likes.find_by(post_id: post_id).destroy
end

Can you give a review of this? I dont know this looks good and its the rails way

I have this controller that I call via Ajax, where I update the likes of a post.

I have two methods in the user model.

already_like_post?: Where I pass the posted ID and return true or false if the post is already liked.

dislike_post: Delete the user like for given post.

class LikesController < ApplicationController
    def create
        if !current_user.already_liked_post? params[:post_id]
            like = Like. new user: current_user, post_id: params[:post_id]
            if !like.save
                render json: like.errors
            end
        else
           current_user.dislike_post params[:post_id]
        end
        amount_of_likes = Post.find(params[:post_id]).likes.count
        render json:{status:"success",likes: amount_of_likes}
    end
end

#These are the methods in the user model:

def already_liked_post?(post_id)
    self.likes.where(post_id: post_id).size == 1
end

def dislike_post(post_id)
    self.likes.find_by(post_id: post_id).destroy
end

Can you give a review of this? I don't know if this looks good or if it is the Rails way.

Source Link
Nivla
  • 53
  • 4

LikesControler for a Post - Rails

I have this controller that i call via ajax, where i update the likes of a post.

I have two methods in user model

already_like_post?: where i pass the post id and return true or false if he already like.

dislike_post: delete the user like for given post.

class LikesController < ApplicationController
    def create
        if !current_user.already_liked_post? params[:post_id]
            like = Like. new user: current_user, post_id: params[:post_id]
            if !like.save
                render json: like.errors
            end
        else
           current_user.dislike_post params[:post_id]
        end
        amount_of_likes = Post.find(params[:post_id]).likes.count
        render json:{status:"success",likes: amount_of_likes}
    end
end

#This are the methods in the user model def already_liked_post?(post_id) self.likes.where(post_id: post_id).size == 1 end

def dislike_post(post_id)
    self.likes.find_by(post_id: post_id).destroy
end

Can you give a review of this? I dont know this looks good and its the rails way