Skip to content

Instantly share code, notes, and snippets.

@marceldegraaf
Created July 21, 2010 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marceldegraaf/484523 to your computer and use it in GitHub Desktop.
Save marceldegraaf/484523 to your computer and use it in GitHub Desktop.
class ItemsController < ApplicationController
include JsonParams
def index
@items = Item.all
respond_to do |format|
format.json { render :json => @items.to_json }
end
end
def create
@item = Item.new(json_params(params))
@item.save!
respond_to do |format|
format.json { render :json => @item, :status => :created, :location => @item}
end
end
def update
@item = Item.find(params[:id])
@item.update_attributes(json_params(params))
respond_to do |format|
format.json { render :json => @item}
end
end
def show
@item = Item.find(params[:id])
respond_to do |format|
format.json { render :json => @item.to_json }
end
end
end
module JsonParams
def json_params(params)
params.reject{|k,v| ignored_fields.include?(k.to_s)}
end
private
def ignored_fields
%w(format action controller id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment