Skip to content

Instantly share code, notes, and snippets.

@igorT
Created July 26, 2011 06:47
Show Gist options
  • Save igorT/1106145 to your computer and use it in GitHub Desktop.
Save igorT/1106145 to your computer and use it in GitHub Desktop.
class Location < ActiveRecord::Base¬
▸ has_many :location_images, :dependent => :destroy¬
▸ has_friendly_id :name, :use_slug => true¬
▸ attr_accessible :cached_slug, :name, :description, :location_images_attributes¬
¬
▸ accepts_nested_attributes_for :location_images, :reject_if => lambda { |t| t['location_image'].nil? }¬
¬
end¬
class LocationImage < ActiveRecord::Base¬
belongs_to :location¬
has_attached_file :photo, :styles => { :small => "150x150>", :large => "320x240>" }¬
validates_attachment_presence :photo¬
¬
¬
end¬
~
~
~
class LocationsController < ApplicationController
# GET /locations
# GET /locations.xml
def index
@locations = Location.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @locations }
end
end
# GET /locations/1
# GET /locations/1.xml
def show
@location = Location.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @location }
end
end
# GET /locations/new
# GET /locations/new.xml
def new
@location = Location.new
3.times {@location.location_images.build}
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @location }
end
end
# GET /locations/1/edit
def edit
@location = Location.find(params[:id])
3.times { @location.location_images.build }
end
# POST /locations
# POST /locations.xml
def create
@location = Location.new(params[:location])
raise params.to_yaml
respond_to do |format|
if @location.save
format.html { redirect_to(@location, :notice => 'Location was successfully created.') }
format.xml { render :xml => @location, :status => :created, :location => @location }
else
format.html { render :action => "new" }
format.xml { render :xml => @location.errors, :status => :unprocessable_entity }
end
end
end
# PUT /locations/1
# PUT /locations/1.xml
def update
@location = Location.find(params[:id])
respond_to do |format|
if @location.update_attributes(params[:location])
format.html { redirect_to(@location, :notice => 'Location was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @location.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /locations/1
# DELETE /locations/1.xml
def destroy
@location = Location.find(params[:id])
@location.destroy
respond_to do |format|
format.html { redirect_to(locations_url) }
format.xml { head :ok }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment