Today me & my friend were working on a simple attachment_fu plugin with the Rails. We had some trouble which we solved it too.
We followed http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu to make the attachment_fu work with our application. Basically we have a company table & attachment table too which has a relation in between. Company table contains the list of companies & Attachment table contains the attachments for the individual companies.
The model looks like this.
class Company < ActiveRecord::Base
has_one :attachment
has_many :books
end
class Attachment < ActiveRecord::Base
belongs_to :company
has_attachment :content_type => ['application/zip']
has_attachment :storage => :file_system, :path_prefix => 'public/files'
validates_as_attachment
end
the view file looks like this
<% form_for(:mugshot, :url => "/companies",
:html => { :multipart => true }) do |f| -%>
<!-- Form -->
<div class="form">
<label>Company Title <span>(Required Field)</span></label>
<input type="text" name="company_title" class="field size1" />
<label>Upload the zip file <span>(Required Field)</span></label>
<%= f.file_field :uploaded_data %>
</div>
<!-- End Form -->
<div class="buttons">
<input type="button" class="button" value="[cancel]" />
<input type="submit" class="button" value="[upload now]" />
</div>
<!-- End Form Buttons -->
<% end %>
The company_controller.rb looks like this.
def create
company = Company.create(:title => params[:company_title], :from => params[:from_date], :to => params[:to_date], :comment => params[:comment])
@zipfile = Attachment.new(params[:uploaded_data])
@zipfile.company_id = company.id
if @zipfile.save
flash[:notice] = 'Company successfully created.'
redirect_to :controller => "books", :action => "new"
else
flash[:error] = 'You must select a file to upload first!'
company.destroy
render :action => :new
end
end
But strangely we got the error saying..
undefined method `stringify_keys!' for
which got rectified based on the changing one line in the view file, from
<%= f.file_field :uploaded_data %>
to
<%= file_field :attachment, :uploaded_data %>
and also update the line in company_controller.rb from
@zipfile = Attachment.new(params[:uploaded_data])
to@zipfile = Attachment.new(params[:attachment])
This made it work properly. Hope this helps you too, else let me know and will try to help you out.
No comments:
Post a Comment