Skip to content

Instantly share code, notes, and snippets.

@jeremywrowe
Created November 1, 2012 03:15
Show Gist options
  • Save jeremywrowe/3991416 to your computer and use it in GitHub Desktop.
Save jeremywrowe/3991416 to your computer and use it in GitHub Desktop.
DRY controller...
class ItemsController < ApplicationController
def index
@items = Item.all
end
def create
@item = Item.new(allowable_params)
update_or_create @item, :save
end
def update
@item = Item.find(params[:id])
update_or_create @item, :update_attributes
end
private
def update_or_create(item, action)
if item.send action, allowable_params
render "items/show"
else
render json: item.errors.messages, status: :bad_request
end
end
def allowable_params
params.require(:item).
permit \
:body, :priority,
:title, :complete,
:position
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment