Skip to content

Commit

Permalink
Adding respond to JSON support for Accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
sebicas committed Nov 30, 2011
1 parent 5884807 commit 4dc5dfb
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AccountsController < ApplicationController
after_filter :update_recently_viewed, :only => :show

# GET /accounts
# GET /accounts.json
# GET /accounts.xml HTML and AJAX
#----------------------------------------------------------------------------
def index
Expand All @@ -30,6 +31,7 @@ def index
respond_to do |format|
format.html # index.html.haml
format.js # index.js.rjs
format.json { render :json => @accounts }
format.xml { render :xml => @accounts }
format.xls { send_data @accounts.to_xls, :type => :xls }
format.csv { send_data @accounts.to_csv, :type => :csv }
Expand All @@ -39,6 +41,7 @@ def index
end

# GET /accounts/1
# GET /accounts/1.json
# GET /accounts/1.xml HTML
#----------------------------------------------------------------------------
def show
Expand All @@ -50,14 +53,16 @@ def show

respond_to do |format|
format.html # show.html.haml
format.json { render :json => @account }
format.xml { render :xml => @account }
end

rescue ActiveRecord::RecordNotFound
respond_to_not_found(:html, :xml)
respond_to_not_found(:html, :json, :xml)
end

# GET /accounts/new
# GET /accounts/new.json
# GET /accounts/new.xml AJAX
#----------------------------------------------------------------------------
def new
Expand All @@ -70,6 +75,7 @@ def new

respond_to do |format|
format.js # new.js.rjs
format.json { render :json => @account }
format.xml { render :xml => @account }
end
end
Expand All @@ -89,6 +95,7 @@ def edit
end

# POST /accounts
# POST /accounts.json
# POST /accounts.xml AJAX
#----------------------------------------------------------------------------
def create
Expand All @@ -102,15 +109,18 @@ def create
@accounts = get_accounts
get_data_for_sidebar
format.js # create.js.rjs
format.json { render :json => @account, :status => :created, :location => @account }
format.xml { render :xml => @account, :status => :created, :location => @account }
else
format.js # create.js.rjs
format.json { render :json => @account.errors, :status => :unprocessable_entity }
format.xml { render :xml => @account.errors, :status => :unprocessable_entity }
end
end
end

# PUT /accounts/1
# PUT /accounts/1.json
# PUT /accounts/1.xml AJAX
#----------------------------------------------------------------------------
def update
Expand All @@ -120,16 +130,18 @@ def update
if @account.update_with_permissions(params[:account], params[:users])
get_data_for_sidebar
format.js
format.json { head :ok }
format.xml { head :ok }
else
@users = User.except(@current_user) # Need it to redraw [Edit Account] form.
format.js
format.json { render :json => @account.errors, :status => :unprocessable_entity }
format.xml { render :xml => @account.errors, :status => :unprocessable_entity }
end
end

rescue ActiveRecord::RecordNotFound
respond_to_not_found(:js, :xml)
respond_to_not_found(:js, :json, :xml)
end

# DELETE /accounts/1
Expand All @@ -142,11 +154,12 @@ def destroy
respond_to do |format|
format.html { respond_to_destroy(:html) }
format.js { respond_to_destroy(:ajax) }
format.json { head :ok }
format.xml { head :ok }
end

rescue ActiveRecord::RecordNotFound
respond_to_not_found(:html, :js, :xml)
respond_to_not_found(:html, :js, :json, :xml)
end

# GET /accounts/search/query AJAX
Expand All @@ -156,6 +169,7 @@ def search

respond_to do |format|
format.js { render :index }
format.json { render :json => @accounts.as_json }
format.xml { render :xml => @accounts.to_xml }
end
end
Expand Down

0 comments on commit 4dc5dfb

Please sign in to comment.