A single form for acts_as_attachment
When I first downloaded the Ruby on Rails plugin acts_as_attachment, I couldn’t understand why the only tutorials I could find did not show how to update the model and the attachment model in the same form. After all, why would you want to put your user through two actions when one would suffice?
I will expand on the DVD Cover example. Some of the code below is blatantly stolen from that tutorial.
Intall the plugin:
> script/plugin source http://svn.techno-weenie.net/projects/plugins
> script/plugin install acts_as_attachment
> rake test_plugins PLUGIN=acts_as_attachment
Generate the Models:
> script/generate model dvd
> script/generate attachment_model dvd_cover
Update your Database:
t.column "name", :string
t.column "studio", :string
t.column "producer", :string
end
create_table :dvd_covers do |t|
t.column "dvd_id", :integer
t.column "content_type", :string
t.column "filename", :string
t.column "size", :integer
# The following fields are not mandatory.
t.column "parent_id", :integer
t.column "thumbnail", :string
t.column "width", :integer
t.column "height", :integer
end
Edit the Models:
class Dvd < ActiveRecord::Base
has_one :dvd_cover
end
class DvdCover < ActiveRecord::Base
belongs_to :dvd
acts_as_attachment :storage => :file_system
validates_as_attachment
end
The Controller:
class DvdController < ApplicationController
def index
@dvd = Dvd.find(:all)
end
def new_dvd
@dvd = params[:id].nil? ? Dvd.new : Dvd.find(params[:id])
@dvd_cover = DvdCover.new(params[:dvd_cover])
if request.post?
@dvd.attributes = params[:dvd]
@dvd_cover.attributes = params[:dvd_cover]
def save_dvd
Dvd.transaction do
@dvd.save!
@dvd_cover.dvd_id = @dvd.id
@dvd_cover.save!
end
end
redirect_to(:action => "index") and return if save_dvd
end
rescue ActiveRecord::RecordInvalid
end
end
The View:
(app/views/new_dvd.rhtml)
<%= error_messages_for :dvd %>
<%= error_messages_for :dvd_cover %>
<%= form_tag({:action => "new_dvd"}, { :multipart=>true }) %>
<label for="name"><strong>Name:</strong></label>
<%= text_field("dvd", "name", "size" => 50, "maxlength" => 50) %>
<label for="name"><strong>Studio:</strong></label>
<%= text_field("dvd", "studio", "size" => 50, "maxlength" => 50) %>
<label for="name"><strong>Producer:</strong></label>
<%= text_field("dvd", "producer", "size" => 50, "maxlength" => 50) %>
<label for="name"><strong>DVD Cover:</strong></label>
<%= file_field("dvd_cover", "uploaded_data") %>
<%= submit_tag ‘Create DVD’ %>
<%= end_form_tag %>
This will give you 1 form to create the DVD and upload the DVD Cover.
June 16th, 2007 at 6:04 pm
Caudata…
Aachen dialect Antonimina Kaupthing Singer & Friedlander Bolingbroke Castle Dixie Browning U.S. Città di Palermo season 2004-05 Soshi kaimei Connington South rail crash Sinsyuu Disbarred (film) …
June 22nd, 2007 at 6:38 am
Plastique_33…
Ulster Scots Enver Pasha Fort Resolution Tusionite Moroni, Utah St. Xavier, MT Frank Field (disambiguation) Chico Xavier Lotus Notes Mail Ogham …
June 23rd, 2007 at 11:35 am
FROM_PROTO-INDO-EUROPEAN_TO_DACIAN…
Nova Scotia Route 340 Gary Mull Hukvaldy, Czech Republic John Pearson (VC) Tyler Burkum Tila Nguyen Cole Ryan Adventure Alabama Theater Harold Leland Goodwin …
July 23rd, 2007 at 2:08 am
Dan…
Cool! Its really cool….
August 23rd, 2007 at 9:17 am
Tyler Technologies | EDEN Division…
Useful, thank you!…
September 3rd, 2007 at 10:17 am
Yakama Sua Kings | 2005 - 2006 Season…
Thank you for your post!…
September 5th, 2007 at 3:44 am
Politics…
The next presidental election should be one of the most interesting ever….
September 6th, 2007 at 10:52 pm
Use MySpace at School…
Anonymous Proxy to unblock myspace and youtube from work or school…
February 19th, 2008 at 9:13 pm
A Guide to Choose Best…
It serves you homely services with cheap rates. Bed and Breakfasts are the main attraction for hotels in Hertford. All…