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



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

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) }
      format.mobile { redirect_to question_url(@answer.question) }
    else
      format.html { render action: "new" }
      format.json { render json: @answer.errors, status: :unprocessable_entity }
      format.mobile { render action: "new" }
    end
  end
end

#destroyObject

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



132
133
134
135
136
137
138
139
# File 'app/controllers/answers_controller.rb', line 132

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



88
89
# File 'app/controllers/answers_controller.rb', line 88

def edit
end

#indexObject

GET /answers GET /answers.json



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
64
65
# File 'app/controllers/answers_controller.rb', line 11

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



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

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



69
70
71
72
73
74
# File 'app/controllers/answers_controller.rb', line 69

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



117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/answers_controller.rb', line 117

def update
  respond_to do |format|
    if @answer.update_attributes(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