Last active
December 15, 2015 20:58
-
-
Save jonkarna/5322085 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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