Friday, June 29, 2007

Difference between capitalize and humanize in Rails

From the script/console:

>> t = "test"
=> "test"
>> t.capitalize
=> "Test"
>> t.humanize
=> "Test"
>> c = "foo bar"
=> "foo bar"
>> c.humanize
=> "Foo bar"
>> c.capitalize
=> "Foo bar"

How to use hidden field in Rails

<%= f.hidden_field :user_id, :value => current_user.id %>

This passes a hidden variable user_id with the value of current_user.id

Thursday, June 28, 2007

Displaying time only in Rails

Not well documented in the Rails API. Here it is:

<%= f.datetime_select(:time, :discard_day => true, :discard_month => true, :discard_year => true) %>

How to create admin accounts in Rails app

The Memphisto source has rake db:bootstrap task that I use for loading admin users into the database. The dynamic fixture allows using any user and password for a admin account.

Wednesday, June 27, 2007

How to freeze gems using Rake

1. script/plugin install http://svn.ardes.com/rails_plugins/gems
2. rake gems:freeze GEM=gem_name

Monday, June 25, 2007

Migrating Local Subversion Repository to Remote Repository

I have been developing the Rails app on my Powerbook using Subversion with file:/// protocol. So when I had to migrate it to Subversion repository hosted remotely by Rimu I had to do the following:

1) On the laptop:

svnadmin dump ~/home/path/to/svn/repos > some-file-name

2) I used Transmit FTP client to use SFTP to the remote host and copied the some-file-name to the remote machine.

3) Login to the remote machine and run:

svnadmin load subversion-repo-name < some-file-name

I created a new directory on my laptop and did a checkout from remote repository:
svn co --username some_user --password some_password http://www.your-domain.com/project_name/trunk

then svn info shows that the repository is http:// pointing to the remote repository.

svn log http://www.your-domain.com/project_name/trunk

shows all the log messages. Note this did not show all the 10 revisions that I had on my local repository. It showed only the changes that I had made after the migration. So the local repository version was at 10 but the remote repository was moving from version 3.

Debugging Rails App in Realtime with the breakpointer

1) include the line: breakpoint "some description goes here" where you want the program execution to stop.
2) From the rails app root directory run: script/breakpointer
3) Fire the browser and let the app hit the breakpoint, you will be taken to the >> prompt where you can examine variables.

This helped me to customize the Attachment_fu plugin so that image upload is optional.

Saturday, June 23, 2007

Testing XML output in Rails

1. get :action_name, :format => "xml", :id => 1
2. content = @response.body
3. assert_select "title", :text => "Rails Recipes"

The above steps basically simulate http://localhost:3000/xmlcontroller/action_name/1.xml.

Let's say you have multiple records in the xml output you can check:

assert_select "book", :count => 2

In this case there will be two books in xml output.

Thursday, June 21, 2007

.svn: Commit failed

Error:
.svn: Commit failed (details follow):
svn: Out of date: 'vendor/plugins' in transaction '7-1'
svn: Your commit message was left in a temporary file:
svn: '/Users/myprojects/svn-commit.tmp'

svn up first and then svn delete on the files that is no longer required and then commit.

Send Method in Ruby

When you have an object, you can set the values for its attributes by:

obj.send(#{attribute}, value) where attribute is the actual attribute while looping through an array of strings that contains the attribute names. The value is the actual value that is used to initialize the attribute.

Wednesday, June 20, 2007

Recursion in Ruby

Calculate x to the power of y using recursion.

def test(x, y)
if (y == 0)
return 1
else
test(x, y-1) * x
end
end

puts test(2,3)

This was a interesting question during the interview. I never understood recursion. The interviewer skillfully provided some clues for me to figure this one out myself. I wrote down some sample values for x and y in a tabular format. Worked through the problem and surprisingly come up with the right answer!

Tuesday, June 19, 2007

No such file or directory - /tmp/501/nl.uu.phil.SSHAgent.socket (Errno::ENOENT)

I was hacking with passwordless ssh login when Capistrano deployment failed with that message. Running (on Mac OS):

$ ssh-agent bash
$ ssh-add

and then running cap tasks fixed the problem for now.

Monday, June 18, 2007

Using acts_as_reportable Rails plugin to generate CSV data from Active Record

1) sudo gem install ruport -y
2) require "ruport" in environment.rb
3) include acts_as_reportable in your model.

You are now ready to play with the model in the script/console. Very easy and powerful plugin. For more info, check out http://stonecode.svnrepository.com/ruport/trac.cgi/wiki/AARTutorial

Upgraded Ruby to Ruby 1.8.6-p36

This version is compatible with FasterCSV. Hivelogic instructions will work, but you need to use the the url ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p36.tar.gz
to install the latest Ruby distribution. This release was announced on Ruby-Talk mailing list.

You can skip the readline installation if you have it already installed for Ruby 1.8.6 released on march 07.

4 Useful Rails Plugins

PDF Plugin
Calendar Helper Plugin:
Active Record Extensions:
Acts As Reportable

Sunday, June 17, 2007

Pending Deployment Tasks

1) Find out how deploy_with_migrations Capistrano switch works.
2) Change the style.css for the site.
3) How to connect to the database during deployment without using database.yml?
4) How to upgrade if you freeze gems?

This is answered in Rails Recipes. 5) Install Exception Notification plugin and SiteMeter.
6) Contact model, create a secure RSS feed. Refer Rails Recipes.

Deployed Rails App on Staging and Production Environments

1) I looked at the mongrel.log file, the error message said that it
could not find staging.rb. So I created an empty staging.rb file under
config/environments directory.

2) staging configuration for socket value was set to the output of
locate mysql.sock.

3) The staging.log showed that require 'turn' statement in
environment.rb was missing the turn gem. I moved this statement to
development.rb, since it is required only for development.

Missing gem headaches: I also learned about rake freeze gem that allows
taking gems to the deployment environment. But I don't know if I freeze
a gem I will be able to upgrade it later. I will get this clarified from
Rails mailing list.

My emails are being sent, but when I signup I see the link as:

http://localhost.localdomain/activate/7fb260146d37cf5aec957fa0662d94c52a9f2488

http://#{ActionMailer::Base.smtp_settings[:domain]}/activate/#{user.activation_code} line in the rhtml template takes the smtp_settings defined for that environment. So
define the domain in staging.rb to fix this problem

Saturday, June 16, 2007

`No such file or directory – /tmp/mysql.sock`

run `locate mysql.sock` or `locate mysqld.sock` to find where your socket is located, then update database.yml to reflect your system’s configuration. So your entry will be:

socket: /path/to/mysql.sock

Friday, June 15, 2007

Simply Rich Association Plugin Released

I am excited to make my first plugin available for public. You can
checkout the code by doing:

svn checkout http://simply-rich-association.googlecode.com/svn/trunk/
simply-rich-association



Hosted on Google Code: Project home page is http://code.google.com/p/simply-rich-association/

The README file has good explanation of what the plugin can do. http://simply-rich-association.googlecode.com/svn/trunk/README
Enjoy!

Wednesday, June 13, 2007

Komodo IDE 4.1 First Impression

When you run the Komodo it loads all the code intelligence for PHP, Python etc. Very annoying when you want to just work on Ruby. After installation the tutorial instructions tell you to load a certain file. Uhh? Where did it install? It does not know where it installed so you have to manually search to load the tutorial.

There is no way to customize the appearance, no themes. No thanks, I will stick with Textmate for now. Maybe Netbeans might be a good alternative one day.

How to convert a name of a class to Class in Ruby

"Foo".constantize will return Foo. Now you can call any methods on Foo.

Attachment_fu - How to display the image

Mike clark's weblog has a good article on how to use attachment_fu to upload images. In my application I did not need any thumbnails. In this case to display the image in the show.rhtml I had to use the image_tag(@person.public_filename) to display the image.

Adding multiple files to Subversion at once

svn st | grep "^?" | awk '{ print $2}' | while read f; do svn add $f; done

If you want all the ? files to be added. Found this tip here http://www.gregwhitescarver.com/blog/2006/05/22/add-multiple-files-in-subversion-at-once/

Creating admin accounts using Rake task

Memphisto project by Rick Olson has a Rake task called db:bootstrap that is in bootstrap.rake file. It creates admin accounts from the data specified in the users.yml file. Awesome!!!

How to export data from one database and import into another.

mysqldump -u root -p myapp_staging some_table > mytable.sql

and then

mysql -u root -p myapp_acceptance < mytable.sql

Rails Refactoring to Resources - Using CRUD and REST in Your Rails Application

Nice book from Apress written by Trotter Cashion. I finished reading this book last night. It has a very good coverage of the RESTful goodness in Rails. Short and sweet. Learned a lot from this book. Very well written. I give it 4.5 out of 5 stars. The reason is ActiveResource in the latest Rails is not discussed in the book.

Saturday, June 09, 2007

svn: Out of date: '/trunk' in transaction '2-1

When I commit the changes to the svn, I got:
svn ci .
Sending .
svn: Commit failed (details follow):
svn: Out of date: '/trunk' in transaction '2-1'
svn: Your commit message was left in a temporary file:
svn: '/Users/balaparanj/work/zepho/ard/svn-commit.tmp'

Out of date here means the repository version is not the same as the local version. It works after doing svn up.