Class: Api::BaseController

Inherits:
ApplicationController show all
Defined in:
app/controllers/api/base_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /api/plural_resource_name



8
9
10
11
12
13
14
15
16
# File 'app/controllers/api/base_controller.rb', line 8

def create
  set_resource(resource_class.new(resource_params))

  if get_resource.save
    render :show, status: :created
  else
    render json: get_resource.errors.full_messages, status: :unprocessable_entity
  end
end

#destroyObject

DELETE /api/plural_resource_name/1



19
20
21
22
# File 'app/controllers/api/base_controller.rb', line 19

def destroy
  get_resource.destroy
  head :no_content
end

#indexObject

GET /api/plural_resource_name



25
26
27
28
29
30
31
# File 'app/controllers/api/base_controller.rb', line 25

def index
  plural_resource_name = "@#{resource_name.pluralize}"
  resources = resource_class.where(query_params)

  instance_variable_set(plural_resource_name, resources)
  instance_variable_get(plural_resource_name)
end

#showObject

GET /api/plural_resource_name/1



34
35
36
# File 'app/controllers/api/base_controller.rb', line 34

def show
  get_resource
end

#updateObject

PATCH/PUT /api/plural_resource_name/1



39
40
41
42
43
44
45
# File 'app/controllers/api/base_controller.rb', line 39

def update
  if get_resource.update(resource_params)
    render :show
  else
    render json: get_resource.errors.full_messages, status: :unprocessable_entity
  end
end