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.

Monday, August 2, 2010

Existing PHP / Ruby not working with MySQL installed with 'sudo port install'

By Default OSX 10.x contains PHP & Ruby installed already. I wanted to make work few applications which used MySQL, PHP & Ruby. So to do this, I installed Xcode & MacPorts too. Then installed mysql using 'sudo port install' and installed successfully. But when I executed the applications, I found that neither PHP nor Ruby could know which mysql.sock can be used to make the existing application work. After a deep search found that port install uses different location and stores the mysql socket. I tried to edit the php.ini file to make sure that it gets connected to the existing socket, but still it didn't worked.

When executed the PHP code I got this warning "Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /Users" but without this solved I can't connect to DB and get the code working.

When I tried with Ruby also it gave me some weird error "ruby `initialize`: wrong number of arguments (4 for 0) (ArgumentError)" and I got frustrated little bit.

So instead of debugging more I thought I can remove the existing PHP & Ruby and install them using the port install command. So I followed the few steps like mentioned below:

sudo mv /System/Library/Frameworks/Ruby.framework/Versions/1.8 /System/Library/Frameworks/Ruby.framework/Versions/1.8.dead

sudo port install ruby
sudo port install rb-rubygems

sudo mv /usr/bin/ruby /usr/bin/old_ruby
sudo mv /usr/bin/gem /usr/bin/old_gem
sudo ln -s /opt/local/bin/ruby /usr/bin/ruby
sudo ln -s /opt/local/bin/gem /usr/bin/gem

sudo gem update --system

sudo gem install xml-simple
sudo gem install mysql

sudo port install php5
sudo port install php5-mysql
sudo port install php5-mbstring

sudo cp /opt/local/etc/php5/php.ini-production /opt/local/etc/php5/php.ini

locate mysqld.sock
==> /opt/local/var/run/mysql5/mysqld.sock (mostly this will be)

sudo vim /opt/local/etc/php5/php.ini

and change all mysql & mysqli default_socket = /opt/local/var/run/mysql5/mysqld.sock

sudo mv /usr/bin/php /usr/bin/old_php

The above commands will rename the existing PHP & Ruby and uses the new Ruby & PHP installed using the port install commands. After updating the php.ini my existing code worked perfectly as though there were no issues. Even the Ruby also didn't gave me any error, even that too worked fantastically.

I hope this will help you to resolve your problem easily rather than debugging the issue which might eat up your time.