Skip to content

Instantly share code, notes, and snippets.

@pt
Created June 21, 2012 15:39
Show Gist options
  • Save pt/2966502 to your computer and use it in GitHub Desktop.
Save pt/2966502 to your computer and use it in GitHub Desktop.
better responses in rails controllers
class ThingController < ApplicationController
def create_suck
@thing = Thing.new(params[:thing])
respond_to do |format|
if @thing.save
format.html { redirect_to @thing, notice: 'thing was successfully created.' }
format.json { render json: @thing, status: :created, location: @thing }
else
format.html { render action: "new" }
format.json { render json: @thing.errors, status: :unprocessable_entity }
end
end
end
def create_awesome
@thing = Thing.new(params[:thing])
response @thing.save do
json(true) { render json: @thing, status: :created, location: @thing }
json(false) { render json: @thing.errors, status: :unprocessable_entity }
html(true) { redirect_to @thing, notice: 'thing was successfully created.' }
html(false) { render action: "new" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment