Archive for July, 2008

Problem with installing Image Science

Posted in Uncategorized on July 30, 2008 by harryche2008

When installing Image Science, it requires FreeImage and Rubyinline. When installing FreeImage, ‘make’ reports some error saying no g++ found.

To install g++, on CentOS, do this:

yum install gcc-c++

Solution to ftp login problem with CentOS vsftp server

Posted in Uncategorized on July 29, 2008 by harryche2008

Sometimes after starting vsftpd service, you still can’t login through ftp. You keep getting errors like:

500 cannot change home directory to …

The problem is with security policy, you need to make sure “Allow ftp to read/write in the user home directories” is checked.

Or, just use

/usr/sbin/setsebool -P ftp_home_dir 1

Solution to error from “gem install mysql”

Posted in Ruby on Rails on July 29, 2008 by harryche2008

On Mac/CentOS, sometimes you get errors when trying to “gem install mysql”.
To fix this, if your Mac is intel based, do:

$ sudo -s
$ ARCHFLAGS=”-arch i386″ gem install mysql — –with-mysql-dir=/usr/local/mysql

you might want to change “/usr/local/mysql” part to whatever is correct on your system.

A few simple ways to debug Javascript code

Posted in Uncategorized on July 25, 2008 by harryche2008

Beyond alert() boxes, here a few very simple ways to debug your javascript code in an Ajax app:

1. Log messages within your js code.
In your html page where js code is running, define perhaps a div for keeping a log of messages that you can append to in your js code, then you can insert this line anywhere in your code, e.g:

$(‘log’).innerHTML += “User entered ” + sUserInput;

Then you can easily show or hide the log by applying a css style to the div tag.

2. Use a logging library, such as Lumberjack or log4js.

3. Venkman debugger

How to run Rails code as a cron job?

Posted in Uncategorized on July 23, 2008 by harryche2008

Rails comes with a script/runner script that can be used to run a specific snippet of code in your Rails app. There’re many ways to organize your code to let this runner script execute it, but one obvious way is to write the code as a model’s action. Here are quick steps for doing this:

1. Insert your code into an action of a model, i.e. MyModel.DoThisThing, DoThisThing must be defined as class method, “def self.DoThisThing…”

2. In your cron job, add a line to run this at certain interval:
/usr/local/bin/ruby /path/to/your/app/script/runner -e production "MyModel.DoThisThing"

Change the ruby path on your system if different.

Loading text(subtitle) as a movie into QuickTime control

Posted in Uncategorized on July 20, 2008 by harryche2008

With working with Apple’s QuickTime Movie control in Visual Studio 2008, you can load a text file as a movie, which is usually used as a subtitle channel added to another video channel.

However, when loading the text file, make sure that the timestamps do not overlap, or not even leave too small an interval between two text lines.

For example:

[00:01:20.45]
Subtitle line 1
[00:01:20.48]
Subtitle line 2

This will not load because the time interval between the two lines is too small, just 0.03 second; the fix is to make the gap bigger, or simply merge the two lines into one, so that they have just one timestamp.

How to do Ajax style drag-n-drop sorting with Rails

Posted in Uncategorized on July 19, 2008 by harryche2008

Here is a quick tutorial on how to do this. The “secret” or easy solution is to use Rails “acts_as_list” plugin.

1. Install the “acts_as_list” plugin that is going to be used for this. With Rails 2.0+, the plugin is no longer part of Rails core, so you will need to do “ruby script/plugin install acts_as_list”.

2. You need to define/add a “position :integer” column to your model that is to be sorted on.

3. Insert the “acts_as_list” directive/declaration in your sortable model, such as:

 
class FoodItem <ActiveRecord::Base
  belongs_to :grocery_list
  acts_as_list :scope => :grocery_list
end

The “:scope” parameter just tells acts_as_list plugin to sort items within the scope of its owner, in this case, within the same grocery_list. In your code, this could be just user, or owner of the stuff you want to sort on.

Next few steps are about how you implement the views to allow users to drag-n-drop sort the list.

4. Define a <ul></ul> section in your view that is going to be used for the dnd sorting:

<ul id="grocery-list" style="cursor: move">
  <% @grocery_list.food_items.each do |f| %>
    <li id="item_<%= f.id %>">
      <%= f.name %>
    </li>
  <% end %>
</ul>

In this example, pay close attention to the tag “id”s, those id values are important for acts_as_list plugin to work its magic.

5. Add following code to your view, same view where the “ul” tag is in:

<%= sortable_element "grocery-list",
:url => {:action => "sort", :id => @grocery_list},
:complete => visual_effect(:highlight, "grocery-list")
%>

What this does is that it generates all needed javascript to make your “ul” list sortable with drag-n-drop, and it reports back your server code with new position information through the :url parameters.

6. Define a backend action to update your model with new position data, such as:

def sort
@grocery_list = GroceryList.find(params[:id])
@grocery_list.food_items.each do | f |
f.position = params["grocery-list"].index(f.id.to_s)+1

f.save
end
end

Then you’re all done.

BTW, I learned this trick from a book called “Rails Recipes”, where there’s more good stuff like this. Check it out if you haven’t.

Have a nice weekend!

Rails class caching issue with production environment

Posted in Uncategorized with tags , on July 19, 2008 by harryche2008

I spent most of yesterday fixing a bug in the code from a ROR project that I am working on. It was finally fixed by turning off config.cache_classes in /config/environments/production.rb:

config.cache_classes = false

By default, that was set to true. Because I have associations in one of models that reference other models, such as:

class User < ActiveRecord::Base
# associations
has_many :goals, :o rder => “created_at desc”
has_many :tasks, :o rder => “created_at desc”
has_many :today_active_tasks, :class_name => “Task”, :conditions => ["status = 'created' AND due >= ? AND due < ? ", Date.today, Date.today+1]

the problem with cache_classes turned on, user.today_active_tasks will get cached, but as you see in the :conditions parameter, this association should return tasks based on comparison to a certain date, which obviously changes everyday. So if this is cached, I will usually get today’s tasks from yesterday’s.

You won’t see this happens with development environment, cause the caching of this nature is not there.

For now, the only solution seems to be just turn off the cache_classes. But perhaps there’s another way to work around this, without having to not cache all classes.

Rails’ flash[:notice] problem

Posted in Uncategorized with tags on July 16, 2008 by harryche2008

I recently found that if you set flash[:notice] in a same controller, but within different actions, it always takes twice refreshes for flash[:notice] to go away. This is annoying because, I would want it to go away after it’s displayed from one action, while the user is at another action view.

The only fix to this seems to be that I have to use different flash[:notice] for different actions, such as flash[:notice1], flash[:notice2], but that still doesn’t seem to be a good solution…

Word: graft

Posted in Uncategorized on July 15, 2008 by harryche2008

Learned a new word today: graft. “graft” means 嫁接。