Created
July 21, 2010 14:00
-
-
Save marceldegraaf/484523 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 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 |
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
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