Class: AnswersController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/answers_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /answers POST /answers.json



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/answers_controller.rb', line 91

def create
  @answer = Answer.new(answer_params)
  @answer.user = current_user
  unless @answer.question
    redirect_to questions_url
    return
  end

  respond_to do |format|
    if @answer.save
      flash[:notice] = t('controller.successfully_created', model: t('activerecord.models.answer'))
      format.html { redirect_to @answer }
      format.json { render json: @answer, status: :created, location: answer_url(@answer) }
    else
      format.html { render action: "new" }
      format.json { render json: @answer.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /answers/1 DELETE /answers/1.json



128
129
130
131
132
133
134
135
# File 'app/controllers/answers_controller.rb', line 128

def destroy
  @answer.destroy

  respond_to do |format|
    format.html { redirect_to question_answers_url(@answer.question) }
    format.json { head :no_content }
  end
end

#editObject

GET /answers/1/edit



86
87
# File 'app/controllers/answers_controller.rb', line 86

def edit
end

#indexObject

GET /answers GET /answers.json



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
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/answers_controller.rb', line 9

def index
  if !current_user.try(:has_role?, 'Librarian')
    if @question
      unless @question.try(:shared?)
        access_denied; return
      end
    end
    if @user != current_user
      access_denied; return
    end
  end

  @count = {}
  if user_signed_in?
    if current_user.has_role?('Librarian')
      if @question
        @answers = @question.answers.order('answers.id DESC').page(params[:page])
      elsif @user
        @answers = @user.answers.order('answers.id DESC').page(params[:page])
      else
        @answers = Answer.order('answers.id DESC').page(params[:page])
      end
    else
      if @question
        if @question.shared?
          @answers = @question.answers.order('answers.id DESC').page(params[:page])
        else
          access_denied; return
        end
      elsif @user
        if @user == current_user
          @answers = @user.answers.order('answers.id DESC').page(params[:page])
        else
          access_denied; return
        end
      else
        access_denied; return
      end
    end
  else
    if @question
      @answers = @question.answers.order('answers.id DESC').page(params[:page])
    else
      access_denied; return
    end
  end
  @count[:query_result] = @answers.size

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @answers.to_json }
    format.rss  { render layout: false }
    format.atom
  end
end

#newObject

GET /answers/new



75
76
77
78
79
80
81
82
83
# File 'app/controllers/answers_controller.rb', line 75

def new
  if @question
    @answer = current_user.answers.new
    @answer.question = @question
  else
    flash[:notice] = t('answer.specify_question')
    redirect_to questions_url
  end
end

#showObject

GET /answers/1 GET /answers/1.json



67
68
69
70
71
72
# File 'app/controllers/answers_controller.rb', line 67

def show
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @answer.to_json }
  end
end

#updateObject

PUT /answers/1 PUT /answers/1.json



113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/answers_controller.rb', line 113

def update
  respond_to do |format|
    if @answer.update(answer_update_params)
      flash[:notice] = t('controller.successfully_updated', model: t('activerecord.models.answer'))
      format.html { redirect_to @answer }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @answer.errors, status: :unprocessable_entity }
    end
  end
end