ruby class

Ruby code posted
created at 02 Aug 19:10

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class CommentsController < ApplicationController
  respond_to :json, :js

  def create
    @comment = Comment.new(comment_params)

    if @comment.save
      mentions_add(
        comment_params['mentions'].uniq,
        'Comment',
        @comment.id,
        current_user.id,
        'Event',
        1
      )
      render json: @comment
    else
      render json: { error: @comment.errors.full_messages }, status: 422
    end
  end

  def update
    @comment = Comment.find(params[:id])
    @comment.update(comment_params)
    render json: @comment
  end

  def show
    @query = Comment.ransack(
      commentable_type_eq: params[:commentable_type],
      commentable_id_eq: params[:commentable_id]
    )
    @comments = @query.result.includes(:user, :votes, pings: :user)
    render json: @comments
  end

  private

  def comment_params
    params.permit(:r, :id, :commentable_id, :commentable_type, :body, :parent_id, :user_id, :edited_at, :votes_count, :created_at, :created_by_current_user, :user_has_upvoted, mentions: [])
  end
end
1.06 KB in 5 ms with coderay