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.

Friday, July 30, 2010

Uninstall Ruby / PHP in existing Mac OS

Hi Guys, posting this after a long period as was little busy ;-). Anyways lets get into the subject.

Whenever OSX 10.x version installed, by default PHP / Ruby will also be installed. This is good but sometimes we don't want the particular version installed so to remove this we need to do the following steps.

For Ruby
sudo mv /System/Library/Frameworks/Ruby.framework/Versions/1.8 /System/Library/Frameworks/Ruby.framework/Versions/1.8.dead
sudo mv /usr/bin/ruby /usr/bin/old_ruby
sudo mv /usr/bin/gem /usr/bin/old_gem

For PHP
sudo mv /usr/bin/php /usr/bin/old_php

Wednesday, April 21, 2010

Drupal: Drush + CCK Import Module Code

I was trying to figure out if there are any modules which I can download for the Drupal 6 and try to use Drush command to do an CCK Import of a file which has been exported thru CCK Export. After a hard dig around, I couldn't find any one, so I started writing my own. Don't worry I have shared the code below. Anyway talks apart, below is the code which you put in a file called drush/commands/cck_import/cck_import.drush.inc




// $Id: cck_import.drush.inc,v 1.1 2010/04/21 03:03:39 kishore Exp $

/**
* @file
* Drush support for cck_import.
*/

/**
* Implementation of hook_drush_command().
*/
function cck_import_drush_command() {
$items = array();

$items['cck_import-import'] = array(
'callback' => 'cck_import_drush_callback_import',
'description' => "Import node code from a previous export.",
'options' => array(
'--uid' => "Uid of user to save nodes as. If not given will use 1. You may specify 0 for the Anonymous user.",
),
'examples' => array(
'drush cck_import import < filename' =>
'Import nodes from given filename.',
),
);

return $items;
}

/**
* Implementation of hook_drush_help().
*
* This function is called whenever a drush user calls
* 'drush help '
*
* @param
* A string with the help section (prepend with 'drush:')
*
* @return
* A string with the help text for your command.
*/
function cck_import_drush_help($section) {
switch ($section) {
case 'drush:cck_import-import':
return dt("Imports nodes that have been exported. Usage: 'drush cck_import import < filename'.");
}
}

/**
* Drush command callback.
*
*/
function cck_import_drush_callback_export() {
$commands = func_get_args();

$nids = array_filter($commands, 'is_numeric');

$data = cck_import_node_bulk($nids, TRUE);

$filename = drush_get_option('file');

if ($filename) {
// Output data to file. Note this only takes a flat filename for the current directory.
// If file exists, ask for whether to overwrite
if (file_exists($filename)) {
if (!drush_confirm(dt('File ' . $filename . ' exists. Do you really want to overwrite?'))) {
return;
}
}
// Write the file.
file_put_contents($filename, $data);
}
else {
// Print to terminal.
drush_print_r($data);
}
}

/**
* Drush command callback.
*
* Import nodes from data.
*/
function cck_import_drush_callback_import() {
$commands = func_get_args();

// Switch to admin or the specified user so imported nodes are not anonymous.
$uid = drush_get_option('uid');
// Test on NULL so uid may be given as 0.
if (is_null($uid)) {
$uid = 1;
}
// uid 0 is already loaded.
if ($uid != 0) {
global $user;
$user = user_load($uid);
}

$values = array();
$values['type_name'] = '';
$values['macro'] = file_get_contents("php://stdin", "r");
$form_state = array();
$form_state['values'] = $values;
if (!empty($form_state)) {
$result = drupal_execute("content_copy_import_form", $form_state);
if ($result === FALSE) {
// There was a problem with types?
drush_set_error('DRUSH_NOT_COMPLETED', 'Problem found with the import file. Check the node types exist.');
}
}

}




After saving this file, try to execute the below command



$ drush cck_import-import < filename.content



note filename.content should contain something which has been exported from the CCK Export.

Hope this would have solved your issues, I am in the process of developing the CCK Import / Export thru Drush commands and might update you soon with other version of the code. Not sure where to publish but thought of publishing in my own blog and in future might publish in a proper particular location where everybody gets benefited.

Feel free to ask me any queries.

Thursday, April 1, 2010

Gmail - April Fool


Hope everybody trying to find out what is the April Fool prank Google is going to play this time. Here G-King played the prank April Fool. GMail application misses all the vowels and wants people to think that there is some virus / some flaw in user's computer.. ;-) might be bad guess..

Anyway here is the screenshot of GMAIL Disemvowelling