Class: Bible270::CommentsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/bible270/comments_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/bible270/comments_controller.rb', line 4

def create
  return unless require_reader!

  @day = params[:day].to_i
  head :bad_request and return unless Plan.valid_day?(@day)

  @comment = current_reader.comments.new(comment_params.merge(day: @day))
  @readings = Plan.readings_for(@day)

  if @comment.save
    respond_to do |format|
      format.turbo_stream
      format.html { redirect_to day_path(@day, anchor: "comment-#{@comment.id}") }
    end
  else
    respond_to do |format|
      format.turbo_stream { render turbo_stream: turbo_stream.replace("new_comment_form", partial: "bible270/comments/form", locals: { day: @day, comment: @comment }) }
      format.html { redirect_to day_path(@day), alert: @comment.errors.full_messages.to_sentence }
    end
  end
end

#destroyObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/bible270/comments_controller.rb', line 26

def destroy
  return unless require_reader!

  @comment = current_reader.comments.find_by(id: params[:id])
  head :not_found and return unless @comment

  @day = @comment.day
  @comment.destroy
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to day_path(@day) }
  end
end