Skip to content

Instantly share code, notes, and snippets.

@patrickberkeley
Created April 28, 2009 19:11
Show Gist options
  • Save patrickberkeley/103320 to your computer and use it in GitHub Desktop.
Save patrickberkeley/103320 to your computer and use it in GitHub Desktop.
Multiple sortable images using Paperclip. Example using a Page model.
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :permalink %><br />
<%= f.text_field :permalink %>
</p>
<p>
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<h2>Images</h2>
<div id="image_listing_wrapper" class='listing_wrapper'>
<%= render :partial => 'shared/images', :locals => {:viewable => @page, :f => f} -%>
</div>
<%= render :partial => 'shared/image_actions', :locals => {:viewable => @page, :f => f} -%>
<h2>Documents</h2>
<div id="document_listing_wrapper" class="listing_wrapper">
<%= render :partial => 'shared/documents', :locals => {:viewable => @page, :f => f} -%>
</div>
<%= render :partial => 'shared/document_actions', :locals => {:viewable => @page, :f => f} -%>
class Asset < ActiveRecord::Base
belongs_to :viewable, :polymorphic => true
acts_as_list :scope => :viewable
end
<h1>Editing page</h1>
<% form_for @page, :html => { :multipart => true, :method => :put } do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<%= render :partial => 'shared/edit_resource_links', :locals => { :collection_url => pages_path } %>
<% end %>
<!-- _edit_resource_links.html.erb -->
<!-- <%= submit_tag "Update", :class => "button" %> or <%= link_to "Cancel", collection_url %> -->
class Image < Asset
has_attached_file :file,
:styles => { :thumb => '75x75#', :small => '250x250>', :medium => '475x475>', :full => '792x792>' },
:default_style => :medium
validates_attachment_content_type :file, :content_type => ['image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif', 'image/tiff']
end
class ImagesController < ApplicationController
# GET /images/new
# GET /images/new.xml
def new
@image = Image.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @image }
end
end
# POST /images
# POST /images.xml
def create
@image = Image.new(params[:image])
respond_to do |format|
if @image.save
flash[:notice] = 'Image was successfully created.'
format.html { redirect_to(@image) }
format.xml { render :xml => @image, :status => :created, :location => @image }
else
format.html { render :action => "new" }
format.xml { render :xml => @image.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /images/1
# DELETE /images/1.xml
def destroy
@image = Image.find(params[:id])
@viewable = @image.viewable
@image.destroy
respond_to do |format|
format.html do
flash[:notice] = nil
render :partial => '/shared/images', :locals => {:viewable => @viewable}
end
format.xml { head :ok }
end
end
end
<h1>New page</h1>
<% form_for @page, :html => { :multipart => true } do |f| %>
<%= render :partial => 'form', :locals => { :f => f } %>
<%= render :partial => 'shared/new_resource_links', :locals => { :collection_url => pages_path } %>
<% end %>
<!-- _new_resource_links.html.erb -->
<!-- <%= submit_tag "Create", :class => "button" %> or <%= link_to "Cancel", collection_url %> -->
class Page < ActiveRecord::Base
has_many :images, :as => :viewable, :order => :position, :dependent => :destroy
validates_presence_of :name, :permalink
end
class PagesController < ApplicationController
after_filter :set_image, :only => [:create, :update]
# GET /pages
# GET /pages.xml
def index
@pages = Page.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @pages }
end
end
# GET /pages/1
# GET /pages/1.xml
def show
if params[:permalink]
@page = Page.find_by_permalink(params[:permalink])
raise ActiveRecord::RecordNotFound, "Page not found." if @page.nil?
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @page }
end
else
@page = Page.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @page }
end
end
end
# GET /pages/new
# GET /pages/new.xml
def new
@page = Page.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @page }
end
end
# GET /pages/1/edit
def edit
@page = Page.find(params[:id])
end
# POST /pages
# POST /pages.xml
def create
@page = Page.new(params[:page])
respond_to do |format|
if @page.save
flash[:notice] = 'Page was successfully created.'
format.html { redirect_to edit_page_path(@page) }
format.xml { render :xml => @page, :status => :created, :location => @page }
else
format.html { render :action => "new" }
format.xml { render :xml => @page.errors, :status => :unprocessable_entity }
end
end
end
# PUT /pages/1
# PUT /pages/1.xml
def update
@page = Page.find(params[:id])
respond_to do |format|
if @page.update_attributes(params[:page])
flash[:notice] = 'Page was successfully updated.'
format.html { redirect_to edit_page_path(@page) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @page.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /pages/1
# DELETE /pages/1.xml
def destroy
@page = Page.find(params[:id])
@page.destroy
respond_to do |format|
format.html { redirect_to(pages_url) }
format.xml { head :ok }
end
end
private
def set_image
return unless params[:image]
return if params[:image][:file].blank?
image = Image.create params[:image] if params[:image]
@page.images << image
end
end
ActiveRecord::Schema.define(:version => 20090317175259) do
create_table "assets", :force => true do |t|
t.integer "viewable_id"
t.string "viewable_type"
t.string "file_content_type"
t.string "file_file_name"
t.integer "file_file_size"
t.integer "position"
t.string "type"
t.datetime "file_updated_at"
end
create_table "pages", :force => true do |t|
t.string "name"
t.string "permalink"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "viewable_id"
end
end
<ul id="image_listing">
<% @page.images.each do |image| %>
<li>
<%= image_tag image.file.url %>
</li>
<% end %>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment