Skip to content

Instantly share code, notes, and snippets.

@ecordell
Created July 22, 2011 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecordell/1099067 to your computer and use it in GitHub Desktop.
Save ecordell/1099067 to your computer and use it in GitHub Desktop.
Started POST "/landscapes" for 127.0.0.1 at 2011-07-22 11:07:18 -0500
Processing by LandscapesController#create as JSON
Parameters: {"landscape"=>{"id"=>0, "name"=>"Newscape", "city"=>"Fun city", "state"=>"LA", "zip"=>"71457", "images_attributes"=>[{"id"=>0, "image_data"=>"Im a bunch of image data image data image data", "is_thumbnail"=>1}], "address1"=>"Cool place"}}
SQL (61.9ms) SHOW TABLES[
Image Load (0.3ms) SELECT `images`.* FROM `images` WHERE `images`.`id` IN (0) AND (`images`.imageable_id = 0 AND `images`.imageable_type = 'Landscape')
Completed in 242ms
ActiveRecord::RecordNotFound (Couldn't find Image with ID=0 for Landscape with ID=0):
app/controllers/landscapes_controller.rb:44:in `new'
app/controllers/landscapes_controller.rb:44:in `create'
class ImagesController < ApplicationController
# GET /images
# GET /images.json
def index
@imageable = find_imageable
@images = @imageable.images
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @images }
end
end
# GET /images/1
# GET /images/1.json
def show
@image = Image.find(params[:id])
@imageable = find_imageable
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @image }
end
end
# GET /images/new
# GET /images/new.json
def new
@image = Image.new
@imageable = find_imageable
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @image }
end
end
# GET /images/1/edit
def edit
@image = Image.find(params[:id])
@imageable = find_imageable
end
# POST /images
# POST /images.json
def create
@imageable = find_imageable
@image = @imageable.images.build(params[:image])
respond_to do |format|
if @image.save
format.html { redirect_to([@imageable, @image], :notice => 'Image was successfully created.') }
format.json { render :json => @image, :status => :created, :location => @image }
else
format.html { render :action => "new" }
format.json { render :json => @image.errors, :status => :unprocessable_entity }
end
end
end
# PUT /images/1
# PUT /images/1.json
def update
@image = Image.find(params[:id])
@imageable = find_imageable
respond_to do |format|
if @image.update_attributes(params[:image])
format.html { redirect_to([@imageable, @image], :notice => 'Image was successfully updated.') }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @image.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.json
def destroy
@image = Image.find(params[:id])
@image.destroy
@imageable = find_imageable
respond_to do |format|
format.html { redirect_to(polymorphic_path([@imageable, :images])) }
format.json { head :ok }
end
end
private
def find_imageable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
end
class LandscapesController < ApplicationController
# GET /landscapes
# GET /landscapes.json
def index
@landscapes = Landscape.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @landscapes }
end
end
# GET /landscapes/1
# GET /landscapes/1.json
def show
@landscape = Landscape.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @landscape.to_json(:include => :images) }
end
end
# GET /landscapes/new
# GET /landscapes/new.json
def new
@landscape = Landscape.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @landscape }
end
end
# GET /landscapes/1/edit
def edit
@landscape = Landscape.find(params[:id])
@landscape.images.build
end
# POST /landscapes
# POST /landscapes.json
def create
@landscape = Landscape.new(params[:landscape])
respond_to do |format|
if @landscape.save
format.html { redirect_to(@landscape, :notice => 'Landscape was successfully created.') }
format.json { render :json => @landscape, :status => :created, :location => @landscape }
else
format.html { render :action => "new" }
format.json { render :json => @landscape.errors, :status => :unprocessable_entity }
end
end
end
# PUT /landscapes/1
# PUT /landscapes/1.json
def update
@landscape = Landscape.find(params[:id])
respond_to do |format|
if @landscape.update_attributes(params[:landscape])
format.html { redirect_to(@landscape, :notice => 'Landscape was successfully updated.') }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @landscape.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /landscapes/1
# DELETE /landscapes/1.json
def destroy
@landscape = Landscape.find(params[:id])
@landscape.destroy
respond_to do |format|
format.html { redirect_to(landscapes_url) }
format.json { head :ok }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment