Class: ActiveRecordApi::Rest::Controller

Inherits:
ApplicationController
  • Object
show all
Includes:
GracefulErrors
Defined in:
lib/active_record_api/rest/controller.rb

Direct Known Subclasses

Auth::Controller

Instance Method Summary collapse

Instance Method Details

#createObject



21
22
23
24
25
26
27
# File 'lib/active_record_api/rest/controller.rb', line 21

def create
  if model.save
    redirect_to_model
  else
    render json: model.errors, status: :unprocessable_entity
  end
end

#destroyObject



37
38
39
40
# File 'lib/active_record_api/rest/controller.rb', line 37

def destroy
  model.destroy!
  head :no_content
end

#indexObject



10
11
12
13
14
# File 'lib/active_record_api/rest/controller.rb', line 10

def index
  response.headers['x-total'] = models.count
  response.headers['x-link-next'] = next_url
  render json: models, each_serializer: serializer
end

#showObject



16
17
18
19
# File 'lib/active_record_api/rest/controller.rb', line 16

def show
  return unless stale?(last_modified: model.updated_at)
  render json: model, serializer: serializer
end

#updateObject



29
30
31
32
33
34
35
# File 'lib/active_record_api/rest/controller.rb', line 29

def update
  if model.update(modifiable_params)
    redirect_to_model
  else
    render json: model.errors, status: :unprocessable_entity
  end
end