Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Created January 10, 2012 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bodacious/1591344 to your computer and use it in GitHub Desktop.
Save Bodacious/1591344 to your computer and use it in GitHub Desktop.
Build a RESTful API in Rails 3 Controller refactored: http://vimeo.com/user7965808/building-a-rest-api-in-rails-3
class UsersController < ApplicationController
# This can be moved to ApplicationController if
# every action within the application should respond to these MIME Types
respond_to :html, :json, :xml
def index
respond_with users
end
def show
respond_with user
end
def create
user.save
respond_with user
end
def update
user.update_attributes(params[:user])
respond_with user
end
def destroy
user.destroy
respond_with user
end
private
# Make these helpers available to the view too
helper_method :user, :users
def user
# If the action is new or create...
if @user = params[:action] =~ /new|create/
User.new(params[:user])
else
User.find(params[:id])
end
end
def users
@users = User.all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment