Skip to content

Instantly share code, notes, and snippets.

@jonkarna
Last active December 15, 2015 20:58
Show Gist options
  • Save jonkarna/5322085 to your computer and use it in GitHub Desktop.
Save jonkarna/5322085 to your computer and use it in GitHub Desktop.
class ApplicationResponder < ActionController::Responder
def to_json
if has_errors?
display_errors
elsif save? || destroy?
render action: :show
else
super
end
end
private
def save?
post? || put?
end
def destroy?
delete?
end
end
class ProductsController < ApplicationController
respond_to :json
self.responder = ApplicationResponder
# def index
# def show
def create
@product = Product.new(product_params)
@product.save
respond_with(@product)
end
def update
@product = find_product
@product.attributes = product_params
@product.save
respond_with(@product)
end
def destroy
@product = find_product
@product.destroy
respond_with(@product)
end
private
# def find_product
# def product_params
end
class ProductsController < ApplicationController
respond_to :json
# def index
# def show
def create
@product = Product.new(product_params)
if @product.save
render action: :show
else
render_errors
end
end
def update
@product = find_product
@product.attributes = product_params
if @product.save
render action: :show
else
render_errors
end
end
def destroy
@product = find_product
if @product.destroy
render action: :show
else
render_errors
end
end
private
# def find_product
# def product_params
# def render_errors
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment