Showing posts with label Attachment. Show all posts
Showing posts with label Attachment. Show all posts

Thursday, August 12, 2010

undefined method `stringify_keys!' for + attachment_fu


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.

Tuesday, September 15, 2009

Missing Attachment in Outlook but present in Web Gmail

While coding send mail functionality, found that the attachment functionality is not working properly. The attachment is displayed in the Web browser Gmail / Mozilla Thunderbird but when tested with Microsoft Outlook, Apple Mail for a strange it didn't showed any attachment. After a long debug we found that instead of

Content-Type : multipart/related;

it should be

Content-Type : multipart/mixed;

I am not sure what exactly the difference but it worked out to be a wonder for us. So Please make sure that whenever Attachment is attached thru coding we should use only

Content-Type : multipart/mixed;

Hope this might help you whenever you face this kind of problem.