-
Notifications
You must be signed in to change notification settings - Fork 0
/
_template_with_form.rb
59 lines (49 loc) · 1.48 KB
/
_template_with_form.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
52
53
54
55
56
57
58
59
class Api::V1::ExamplesController < Api::V1::ApplicationController
before_action :set_example, only: %i[show update destroy]
def index
@examples = current_user.examples
# or for policies
# @examples = policy_scope(Example)
end
def show; end
def create
example_form = ExampleForm.new(example_params, current_user)
if example_form.save
@example = example_form.example
else
errors = example_form.errors
render json: { errors: errors }, status: :bad_request
end
end
def update
example_form = ExampleForm.new(example_params.merge(example: @example), current_user)
if example_form.save
@example = example_form.example
else
errors = example_form.errors
render json: { errors: errors }, status: :bad_request
end
end
def destroy
if @example.destroy
head :ok
else
errors = @example.errors
render json: { errors: errors }, status: :bad_request
end
end
private
def set_example
@example = current_user.examples.find_by_id(params[:id].to_i)
# or for policies
# @example = policy_scope(Example).find_by_id(params[:id].to_i)
# if you use reference_id use .find_by_reference_id
unless @example
# For security reasons even if record is not found return unauthorized
render json: { errors: [UnauthorizedError.new] }, status: :unauthorized
end
end
def example_params
params.permit(:param_1, :param_2, :param_3)
end
end