Created
June 21, 2012 15:39
-
-
Save pt/2966502 to your computer and use it in GitHub Desktop.
better responses in rails controllers
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 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