-
Notifications
You must be signed in to change notification settings - Fork 0
/
questions_controller.rb
51 lines (40 loc) · 1.05 KB
/
questions_controller.rb
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
43
44
45
46
47
48
49
50
51
class QuestionsController < ApplicationController
before_action :authenticate_user!, except: %i[index show]
before_action :set_question, only: %i[show edit update destroy]
def index
@questions = Question.all
end
def show
@answer = Answer.new
end
def new
@question = Question.new
end
def edit
end
def create
@question = Question.new(question_params)
@question.user = current_user
if @question.save
redirect_to @question, notice: "Your question was successfully created."
else
render :new
end
end
def update
return head :forbidden unless current_user.owner?(@question)
@question.update(question_params)
end
def destroy
return head :forbidden unless current_user.owner?(@question)
@question.destroy
redirect_to questions_path, notice: "Your question was successfully deleted!"
end
private
def set_question
@question = Question.with_attached_files.find(params[:id])
end
def question_params
params.require(:question).permit(:title, :body, files: [])
end
end