I want to use attachment_fu to handle audio file uploads.
In the rails root, in the command-line
script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/
and
script/generate Model Audio
Edit the newly created migration in db/00X_Create_Audios.rb
class CreateAudios < ActiveRecord::Migration
def self.up
create_table :audios do |t|
t.column :parent_id, :integer
t.column :content_type, :string
t.column :filename, :string
t.column :size, :integer #thanks KNK
end
end
def self.down
drop_table :audios
end
end
Run the migration to create the database
rake db:migrate
Inside the audio.rb Model file
class Audio < ActiveRecord::Base
has_attachment :content_type => ‘application/mp3′,
:storage => :file_system,
:max_size => 10.megabytes,
:processor => :none
}
#validates_as_attachment #i’ll do this later
end
Create the controller
script/generate Controller Audio
def new
@audio = Audio.new
end
def create
@audio = Audio.new(params[:audio])
if @audio.save
flash[:notice] = 'Audio was uploaded.'
render :action => :new
#redirect_to audio_url(@audio)
else
flash[:notice] = 'error'
render :action => :new
end
end
Make a new file inside app/views/layouts called audio.rhtml, I copied from another layout file, but at the least put a yield command
<%= yield %>
Make a directory inside the app/views/ called audio
Create a new view file inside a newly created audio folder called new.rhtml
Upload Song
<%= error_messages_for :audio %>
<% form_for (:audio, @audio, :url => { :action => “create” }, :html => { :multipart => true }) do |f| -%>
<%= f.file_field :uploaded_data %>
<%= submit_tag 'Upload Audio File' %>
<% end -%>
Don’t forget your routes in config/routes.rb
map.connect 'audio/new', :controller => 'audio', :action => 'new'
map.connect 'audio/create', :controller => 'audio', :action => 'create'
That’s pretty much it to get it working.
There is really no documentation, but if you browse the code you’ll see a lot of how you are supposed to configure it.
Credits: I pretty much modified Mike Clark’s Attachment_fu tutorial to suit my needs, so big up Mike! and of course, the rails plugin master, Rick Olson.
Other links: Wiki | Options & Examples | merb
Bug fixes: KNK for pointing out t.size migration problem
14 responses so far ↓
1 Tomas // Mar 29, 2007 at 6:43 pm
Hi. Thanks for the tutorial.
I’m running into the following error after clicking the upload button:
undefined method `size=’ for #
Would you know how to fix this?
Thanks.
2 Thomas Swift // Mar 30, 2007 at 12:30 am
I had that happen to me as well, but it was because i wasn’t defining a :size or :max_size parameter inside of the model.
do you have a :size or :max_size set in the model?
3 KNK // May 27, 2007 at 7:52 pm
I am now encoutering this problem and I have defined max_size and size but yet neither of them work. I get the same error. Something I missed?
4 KNK // May 31, 2007 at 11:35 pm
Nevermind false alarm
got it figured out today. All I had to do was have:
t.column “size”, :integer
In the migration file, the tutorial’s migration file is missing that. Once I added that it started working just fine.
5 Thomas Swift // Jun 1, 2007 at 12:05 pm
I am going to update the tutorial as well.
but in case anyone is following this
You need a size column, just like KNK said.
I originally didn’t put this in there, but it has been updated.
6 David Johnson // Sep 4, 2007 at 8:29 pm
I am trying to use it and made a huge amount of headway, but now I’m getting
undefined method `uploaded_data=’ for #
I am new to Rails, so I am betting I am doing something stupid, but I can’t find any reference to if there is an require missing or something.
7 Levi // Sep 5, 2007 at 9:04 am
Hey,
Good tutorial. There is a lot missing on most of the tutorials on this plug in…you covered things that i had to figure out the hard way…
For example…you commented out “redirect_to audio_url(@audio)”, I always got a no method error(i think, its been a few days). I just changed that to:
redirect_to action => ‘list’
Thats really what I wanted it to do anyway…
Just out of curiosity…how does one destroy the file once its uploaded? That is the only problem I am having…I just got into rails, and server side scripting about 1 month ago…so I am having some issues with reading some of the source and figuring out what to do with it…
I found the destroy_file method in the file_system_backend.rb file, I just don’t know how to put it into practice.
Would love to hear back from you. Thanks a lot!
8 Levi // Sep 5, 2007 at 12:09 pm
Just figured it out…nevermind…
9 Thomas Swift // Sep 5, 2007 at 2:29 pm
@Levi, glad i could help.
@David, I’m a little busy at the moment, but I’ll take a look when I get a chance.
10 rick // Sep 22, 2007 at 8:18 am
Have you looked at streaming audio with Ruby on Rails? I will looking into this soon.. any suggestions?
11 Edward // Sep 25, 2007 at 7:11 pm
Hello all. I’m having an issue with the migration file. I’m using MacOSX with Locomotive and MAMP, and I’m getting the following error when I do rake db:migrate:
rake aborted!
./db/migrate//001_create_audios.rb:1: syntax error, unexpected ‘:’, expecting ‘\n’ or ‘;’
end endrop_table :audios:integeringringation
Any help would be great!
Thanks!
12 Chuong Huynh // Oct 9, 2007 at 9:50 pm
Like David, I have the same error of getting error “undefined method `uploaded_data=’ for #…”. I checked carefully all steps. Someone has the same problem and know how to fix? Thanks
13 Thomas Swift // Oct 10, 2007 at 1:17 am
@Chuong are you calling the file_field something else, because I think attachment_fu needs it to be called uploaded_data.
Maybe that helps?
14 edgarjs // Oct 18, 2007 at 11:23 am
Ok you saved my life. Thanks!
Leave a Comment