Tuesday, September 28, 2010

Rails respond_to and IE File Download Security Warning

As discussed in my earlier post, I am working on a WEB API integration project involving Radiant. This is a public facing website and requires full support of Internet Explorer. So far, IE has proved to be a great pain. I have got an IFRAME rendering pages from another web application. Now, this application had a controller code with respond_to block.

def method_name
#some RUBY code
respond_to do |format|
format.js {#render some JSON}
format.html {#redirect to somewhere}
end
end

Now, everything seemed to be working good on FireFox and Chrome. Next was the turn of IE. As expected, MORE PROBLEMS.....!! It didn't work!!! It started showing File Download Security Warning Dialog. I had no clue what it was doing. I was totally stumped. Finally, after a bit of googling I came across this post. It saved my day.

As mentioned in the post, I tried printing the request.format for normal HTML request. To my surprise, IE was registering the Mime::Type to be "image/gif". Also, format.html? was nil.

I tried accessing the same action from FireFox. This time, the request format was set correctly to be "text/html".

Now, there is no Mime::Type registered for "image/gif". Hence, first accept type was taking preference. The solution (moving format.html before format.js) mentioned in the post worked for me.

Wednesday, August 11, 2010

undefined method `rewrite' for <#String: URL_STRING>

Currently, I am working on a WEB API integration project for a leading tourism company in Australia. I am using RADIANT, MySQL, Ruby 1.8.7 and Rails 2.3.8. While working on an issue, I ran into a weird error: undefined method `rewrite' for <#String URL_STRING>.

I was defining an instance variable @url in the controller. Also, I was using a simple form_for tag in the view.

CONTROLLER:

class SomeController < ApplicationController
def some_method
#some logic
@url = URL_STRING
end
end


VIEW:

- form_for :some_object, :url => @url do |f|
- #some form fields
= submit_tag 'submit'


Everything seemed normal. But, I started getting this weird error. After a bit of search, I came to know that link_to, url_for and similar method uses ActionController::Base @url instance variable to generate URLs. So, make sure that you don't use/define @url anywhere in your controllers/helpers.

Monday, July 26, 2010

Commands...I always forget


  • Locating a file: sudo find / -name my-large.cnf -print
  • Disk usage: df -kh

MySQL gem installation problem (Mac OS X - 10.5.8, RVM, Ruby 1.9.1)

As mentioned in my previous post, I have been working with RVM on Mac OS X. I have had real troubles installing MySQL gem with ruby-1.9.1p378 on RVM. I tried different things:


  • Removed RVM setup using rvm implode

  • env ARCHFLAGS="-arch x86_64" gem install mysql --verbose -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

  • Re-installed MySQL through DMG archive

  • Tried building MySQL gem on my machine



Every single time I was let down. I got the same error every single time:



ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.

/Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/bin/ruby extconf.rb --with-mysql-config=/usr/local/mysql/bin/mysql_config
checking for mysql_ssl_set()... yes
checking for rb_str_set_len()... no
checking for rb_thread_start_timer()... no
checking for mysql.h... yes
creating Makefile

make
gcc -I. -I/Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/i386-darwin9.8.0 -I/Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/ruby/backward -I/Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1 -I. -DHAVE_MYSQL_SSL_SET -DHAVE_MYSQL_H -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -I/usr/local/mysql/include -g -Os -arch x86_64 -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL -fno-common -O2 -g -Wall -Wno-parentheses -fno-common -pipe -fno-common -o mysql.o -c mysql.c
In file included from /Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/ruby.h:32,
from mysql.c:5:
/Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/ruby/ruby.h: In function 'INT2NUM':
/Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/ruby/ruby.h:464: warning: comparison is always true due to limited range of data type
/Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/ruby/ruby.h:464: warning: comparison is always true due to limited range of data type
/Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/ruby/ruby.h: In function 'UINT2NUM':
/Users/BLAH/.rvm/rubies/ruby-1.9.1-p378/include/ruby-1.9.1/ruby/ruby.h:472: warning: comparison is always true due to limited range of data type
mysql.c: In function 'escape_string':
mysql.c:290: error: lvalue required as left operand of assignment
mysql.c:290: error: lvalue required as left operand of assignment
mysql.c: In function 'real_escape_string':
mysql.c:434: error: lvalue required as left operand of assignment
mysql.c:434: error: lvalue required as left operand of assignment
make: *** [mysql.o] Error 1



Finally, I consulted a colleague (David) at work. He suggested me to install MySQL via ports. Guess what???!!! He was spot on....!!

SOLUTION:
All I had to do was:


  • Install MySQL via ports

  • env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/opt/local/bin/mysql_config5


Monday, April 19, 2010

Efficient multiple SQL inserts in RAILS - AR Extensions

Recently, I was required to generate multiple inserts in a RAILS project. I went with traditional way of looping through and creating AR objects.


#class definition
class CartoonCharacter < ActiveRecord::Base
end

#values for NEW objects
names = ["Tom", "Jerry", "Donald"]

#loop thru and create objects
names.each do |name|
CartoonCharacter.create!(:name => name)
end


It's not as bigger problem if you're just creating 2 or 3 records. Imagine you want to create 70,000 records. I was required to do the same. This approach killed my machine and it just took ages. I couldn't wait after 5 minutes and killed the process.

I started looking out for other options. I came across ar-extensions.

It is a nice gem for mass import and conversion of data to CSV. It also incorporates better finder support. All I've used is its mass import functionality.

Usage:

column_names = [:name, :age, :country]
values = []

#build up values
15000.times do
values << ["Tom", "65", "AU"]
end

#import
CartoonCharacter.import column_names, values


That's it. It worked like a charm. Yes, please don't forget to include adapter specific functionality.


require 'ar-extensions/adapters/mysql'
require 'ar-extensions/import/mysql'

Recently, some work has been done to extract the import functionality of ar-extenstions into activerecord-import.

More information available at: http://continuousthinking.com/tags/arext

Thursday, December 3, 2009

Java ME Early Access to rescue Mac J2ME Developers

Finally, we have an IDE for MAC J2ME developers. Most of the Mac users use MPowerPlayer SDK(mpp-sdk) to develop J2ME applications. It was hell of a pain to use it with NetBeans. It also lacked the Bluetooth API support.

Last month, Sun announced the launch of most awaited JAVA ME SDK 3.0. This can be downloaded at http://java.sun.com/javame/downloads/sdk30_mac.jsp. Please note, it is an Early Access release(not beta). So, it is expected to have some issues. Couple of issues I am aware of are:
  • Exceptions when launching JAVA ME SDK (Just ignore all the exceptions hitting 'Cancel')
  • Also, it runs out of memory after every 4-8 runs and will require Mac reboot(YES, mac reboot).
You might have to restart your Mac immediately after JAVA ME SDK installation.

Apart from these issues, it works like a MAGIC. It has got a nice environment based on NetBeans platform. I have tried mpp-sdk and Java WTK within Sun's Virtual Box, but I find JAVA ME EA to be the best.

Sunday, November 29, 2009

Frequently used git commands

Ignoring all the local uncommitted changes
$ git reset --hard


Ignoring changes to individual files
$ git checkout path_to_file


Tracking a remote branch (Creates a new local branch based on remote branch and switches to that branch)
$ git checkout --track -b local_name origin/remote_name


Deleting remote branch/tag
$ git push origin :heads/branch_name
$ git push origin :branch_name
$ git push origin :tag_name

$ git push origin :refs/tags/tag_name



Deleting local tag
$ git tag -d tag_name


Deleting local branch (already merged)
$ git branch -d branch_name


Deleting local branch (unmerged)
$ git branch -D branch_name


Merging with desired merge tool
$ git mergetool --tool=tool_name


Clearing stash
$ git stash clear


Reverting a commit
$ git revert HASH


Deleting untracked files
$ git clean -n(for dry-run) -f(for forceful clean)

Thursday, September 24, 2009

Starting and stopping MySQL on Mac OS X Leopard

sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
sudo /Library/StartupItems/MySQLCOM/MySQLCOM stop

Reference: Simplistic Complexity

Long running processes

In our application, we've got number of long running processes. These processes are meant to run in background without affecting the application response time. We are using the combination of starling and workling.

For installing starling-starling:
gem sources -a http://gems.github.com/
gem install starling-starling