Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

using Paperclip for multiple attachments and displaying them using the method described in Advanced Rails Recipe #13

View pages_controller.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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
View pages_controller.rb
1 2 3 4 5
def add_asset_link(name)
link_to_function name do |page|
page.insert_html :bottom, :assets, :partial => 'asset', :object => Asset.new
end
end
View pages_controller.rb
1 2 3 4 5
class Asset < ActiveRecord::Base
belongs_to :page
has_attached_file :file, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
View pages_controller.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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
View pages_controller.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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
View pages_controller.rb
1 2 3 4 5 6 7 8 9 10 11
<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>
View pages_controller.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<%= 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 %>
View pages_controller.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<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>
View pages_controller.rb
1 2 3 4 5 6 7 8 9
<!-- 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' } %>
View pages_controller.rb
1 2 3 4 5 6 7 8 9 10
<%= 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 %>

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
Something went wrong with that request. Please try again.