Skip to content

Instantly share code, notes, and snippets.

@patrickberkeley
Created December 7, 2008 02:10
Show Gist options
  • Save patrickberkeley/33011 to your computer and use it in GitHub Desktop.
Save patrickberkeley/33011 to your computer and use it in GitHub Desktop.
using Paperclip for multiple attachments and displaying them using the method described in Advanced Rails Recipe #13
<div class="asset">
<% new_or_existing = asset.new_record? ? 'new' : 'existing' %>
<% prefix = "page[#{new_or_existing}_asset_attributes][]" %>
<% fields_for prefix, asset do |asset_form| -%>
<p>
Asset: <%= asset_form.file_field :file %>
<%= link_to_function "remove", "$(this).up('.asset').remove()" %>
</p>
<% end -%>
</div>
<%= error_messages_for :page %>
<% form_for @page, :html => { :multipart => true } do |f| %>
<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>
<div id="assets">
Attach a file or image<br />
<%= render :partial => 'asset', :collection => @page.assets %>
</div>
<p>
<%= add_asset_link "Add a file" %>
</p>
<p>
<%= f.submit button_name %>
</p>
<% end %>
<%= link_to 'Cancel', pages_path %>
class Asset < ActiveRecord::Base
belongs_to :page
has_attached_file :file, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
<!-- new.html.erb -->
<h1>New page</h1>
<%= render :partial => 'form', :locals => { :page => @page, :button_name => 'Create' } %>
<!-- edit.html.erb -->
<h1>Editing page</h1>
<%= render :partial => 'form', :locals => { :page => @page, :button_name => 'Update' } %>
<div class="side-nav actions">
<%= link_to "New", new_page_path if logged_in? %>
</div>
<%= title('Pages', :h1)%>
<div id="pages">
<% for page in @pages %>
<div class="page">
<%= render :partial => 'shared/side_nav', :locals => { :obj => page, :edit_obj => edit_page_path(page) } %>
<div class="main">
<h2><%= page.name %></h2>
<div class="content"><%= truncate(page.content) %></div>
</div>
</div>
<% end %>
</div>
class Page < ActiveRecord::Base
has_many :assets, :dependent => :destroy
validates_associated :assets
validates_presence_of :name, :permalink
after_update :save_assets
def new_asset_attributes=(asset_attributes)
asset_attributes.each do |attributes|
assets.build(attributes)
end
end
def existing_asset_attributes=(asset_attributes)
assets.reject(&:new_record?).each do |asset|
attributes = asset_attributes[asset.id.to_s]
if attributes
asset.attributes = attributes
else
asset.delete(asset)
end
end
end
def save_assets
assets.each do |asset|
asset.save(false)
end
end
end
class PagesController < ApplicationController
before_filter :login_required, :except => [ :show ]
# GET /pages
# GET /pages.xml
def index
@pages = Page.find(: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
@page.assets.build
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(@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
params[:page][:existing_asset_attributes] ||= {}
@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(@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
end
def add_asset_link(name)
link_to_function name do |page|
page.insert_html :bottom, :assets, :partial => 'asset', :object => Asset.new
end
end
create_table "assets", :force => true do |t|
t.string "name"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.datetime "file_updated_at"
t.integer "page_id"
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"
end
<%= render :partial => 'shared/show_side_nav', :locals => { :obj => @event, :edit_obj => edit_page_path(@page) } %>
<%= title(@page.name, :h1) %>
<%= simple_format h(@page.content) %>
<% for asset in @page.assets %>
<p><%= image_tag asset.file.url %></p>
<p>Small:<%= image_tag asset.file.url(:medium) %></p>
<% end %>
@ddoolin
Copy link

ddoolin commented Dec 26, 2013

Looks good, but missing the strong parameters in the PageController. Also, link_to_function is being deprecated/removed (or already is in 4.1?), and it should be asset.save(:validate => false).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment