Archive for the 'Ruby on Rails' Category

Rails and readline problems on Mac OS X

Friday, October 19th, 2007

If you build Ruby on Mac OS X yourself, you may face the following problem once you have downloaded the language's source from ruby-lang.org, compiled/installed it and tried to run your Rails app:

 mike$ script/console

Loading development environment.

dyld: NSLinkModule() error

dyld: Symbol not found: _rl_filename_completion_function

  Referenced from: /usr/local/lib/ruby/1.8/i686-darwin9.0.0b5/readline.bundle

  Expected in: flat namespace

 

Trace/BPT trap 

 

 In order to fix the problem, you need to re-build readline from the source and then rebuild Ruby, linking it to the new readline's installation dir. Here's how you do it:

wget ftp://ftp.gnu.org/gnu/readline/readline-5.1.tar.gz 

tar -xzf readline-5.1.tar.gz

cd readline-5.1

./configure –prefix=/usr/local && make && sudo make install

 

and then rebuild Ruby (you do it in your Ruby source directory of course)

make distclean && configure –with-readline-dir=/usr/local && make && make install   

Once done, everything should be back on track. 

New portal goes online

Tuesday, October 2nd, 2007

Being in development (mostly contents part, not the system itself) for a several months, a kimono-company portal which uses the multi-site system I have developed, have finally went live today :)

The Kyo-roman portal is a portal system for a company which sells kimonos here in Japan - the headquarters and filials (and that's 17 of them!) each got own web-site, with blog and news/events periodicals, centralized articles approval solution and other perks like visual editor and video uploads. Latest news from filial sites are all gathered on the top page of the headquarters' site (hey, it's a portal site, after all :).

Currently, sites of company's filials are pretty minimalistic, but hopefully that will change in the future (it's up to users to add new content from now on).

The system uses Ruby on Rails as development system (specifically, version early version of my Rails-based multi-site system) and is deployed on CentOS-based hosting, Apache 2 + Mongrel cluster.

Well I know this might sound pretty boring (bah.. ANOTHER system online, so what?), but this is actually the first large-scale deployment of the system I've been working on for over a year, so well.. this is kinda exciting for me :)

One more Rails project online

Thursday, September 20th, 2007

I have another project went live.

RightViewPro is a site for baseball fans, selling some pretty interesting baseball systems. The site is written completely in Rails, features rich admin interface, fully customizable thru admin panel, and features online shop with physical and downloadable products.

Cheers goes to WindUp Design team with whom I was working on the project.

Override :destroy parameter in acts_as_tree in Rails

Thursday, September 20th, 2007

I have made a little extension (just function redefinition, actually) to allow you to control the behaviour of :destroy parameter in acts_as_tree function in Rails. More info, and download file follows.

This extension redefines the default behaviour of acts_as_tree function
to allow you to override the :dependent option for your trees
The default value defined in Rails is :destroy, but sometime you might want to use
a different cascade delete logic, or don't use cascade deleted at all.

Usage:
1. put this file into /lib/active_record_ext/ folder inside your rails application
2. add the following string somewhere in the environment.rb file:
require 'active_record_ext/tree'

This fix implements patch submitted to Rails development site, but not yet implemented
in main trunk of Rails. More info available at this link.

This extension should work without problems on Rails 1.2x, but of course I can not be held responsible
for any damage/loss/etc it might cause.

Download the extension patch (2Kb) 

Customizing :scope in acts_as_list

Monday, August 27th, 2007

acts_as_list is another example of poorly documented functionality in Ruby on Rails (I wonder when we can have a good, examples-rich documentation for RoR? php.net's approach will work perfectly). This blog entry will show how to use :scope in case you want to match it against string/varchar columns.I will cite the acts_as_list documentation right here:

acts_as_list(options = {})Configuration options are:
  • column - specifies the column name to use for keeping the position integer (default: position)
  • scope - restricts what is to be considered a list. Given a symbol, it‘ll attach "_id" (if that hasn‘t been already) and use that as the foreign key restriction. It‘s also possible to give it an entire string that is interpolated if you need a tighter scope than just a foreign key. Example: acts_as_list :scope => ‘todo_list_id = #{todo_list_id} AND completed = 0‘

Well yeah. It tells you that you can customize scope with more than a single parameter. And the example is the simplest one - it uses just integer-based columns for matching. But what is you want to match scope agains a string value? This becomes a little tricky. I remember spending alot of time trying to understand why the bloody code just doesn't work (throwing errors at me).

Here's a little example of how you would use :scope when you want to match against several parameters, with some parameters being string values, and not integers.Say, you have the following structure: a list of pages, each of the page belongs to a category using "category_id" column, and also has a "page_type" determining a page type, which is a string value (ex: 'normal_page', 'inquiry_page', etc..). And say, you want to make these pages into an ordered list, so pages are ordered inside a category. This is simple. You just write:

acts_as_list :scope => :category

there's another possible syntax for the same functionality:

acts_as_list :scope => 'category_id=#{category_id}'

and you're done. But! What if you want pages of different types inside the same category to be ordered individually (or just add the ordering to a subset of pages)? This way you want a double-scoping by category_id and page_type. And page_type being a varchar/string-based column. How to do that kind of scoping? Here's how:

acts_as_list :scope => 'category_id=#{category_id} and page_type=\'#{page_type}\"

Please note that it seems (at least from my tests) that only single-quote characters can be used. No, you can't use double-quote characters inside single-quoted string, and no, you can't do otherwise. You must enclose the :scope definition in single quotes, and use escaped single-quotes when you want to match the scope against char table column.Well, the entry turned out to be about 10x longer than originally planned.. But hopefully it will save you an hour or so trying to figure out how to make the bloody scoping thing work.

Outputting resized attachment_fu image, stored in database, with RMagick

Tuesday, July 10th, 2007

I have recently started using attachment_fu plugin to store attachments (including images, of course) in database. attachment_fu provides functionality to create thumbnail images during upload, but you can't really foresee what thumbnail sizes you will need down the road, can't you?

That's why I decided not to make any thumbnails at all during the upload stage (except for basic resizing so I will not end up with 4K-pixels images in database.

I store all the images in database (easier to migrate, easier to backup) , which adds some level of complixity when you try to output stuff to the user.

Having all that, I wanted to be able to easily output page_cachable resized images. Here's how I have implemented it. (more…)

Using PHP inside Rails structure on Apache

Friday, June 29th, 2007

I do alot of development in Rails recently, and it all goes fine and nice, and Rails is fast and easy to use, but sometimes I'm better off using already existing PHP scripts. Just place them into Rails project, and they just work (think of PHP-based web forms etc).

How do you setup your Apache & Mongrel cluster based Rails project to support execution of PHP scripts inside the Rails project structure? Very easy, actually. (more…)

Dynamic setup of caches_page in Rails

Thursday, May 17th, 2007

Hot off the press! :) This blog entry will show you how to dynamically setup page caching in Rails, when you have a constantly growing list of actions you want to cache, but don't want to update caches_page's actions listing every time you add a new action which requires caching.. This is especially handy if you use FlexImage plugin for Rails. Read on if you're interested. (more…)

Rails fragment caching (and Rails caching in general)

Tuesday, May 8th, 2007

Well I finally had some time to spare in order to implement a decent and easy to use caching system into my first, and therefore most loved, Rails project.

There is a plenty of information on the net regarding Rails caching, but somehow, too much info turned out into having no info I could readily use at all :) I was even writing about caching in Rails myself in this post: "Simple and fast caching in Rails". But I definitely can't say I'm a Rails caching expert in any way though.

For those in the same boat as myself, here's a simple intro into Rails caching (mostly in form of external links), and possible approaches. (more…)

How to install Ruby, Rails, mySQL and Apache from (almost) the scratch

Tuesday, April 3rd, 2007

I've been installing everything that is needed to run Ruby on Rails application onto a host (which is CentOS 4.x), which didn't even have Ruby installed, mySQL was too old and Apache didn't have required modules for running Mongrel clusters. So I had to install everything from the scratch, and the whole process was documented so I could repeat it in the future :) But may be this will helpful to someone else too, so here it is, right from my Yojimbo's note :)
Just one thing to notice. CentOS already has some of the required libs installed (zlib, libpng, libjpeg and freetype), so if you don't have them installed, do it before proceeding. I think there are plenty of info on this matter so I'll omit it.

Now, to the business.

RUBY

wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.5.tar.gz
tar zxvf ruby-1.8.5.tar.gz
cd ruby-1.8.5
./configure
make
sudo make install

Warning! If you're on Mac, you have to use the following string to configure Ruby install:

./configure –prefix=/usr/local –enable-pthread

On Mac OS X, once you have installed Ruby, you have to fix default paths for your default shell, because Ruby installs itself to /usr/local/bin by default, and Apple's default binaries location is in /usr/bin. Do this in Terminal:

pico ~/.bash_login

# and paste the following text:
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
# save the file (Control-W) and relogin, or just re-open terminal session before proceeding.

RAILS : GEMS

wget http://rubyforge.rubyuser.de/rubygems/rubygems-0.9.2.tgz
tar zxvf rubygems-0.9.2.tgz
cd rubygems-0.9.2
ruby setup.rb

RAILS: CORE (possible to use –include-dependencies, but i was getting some 'not found' error with this option)

#install the specific version, which happens to be 1.2.2 in this case.
gem install -v=1.2.2 rails

RAILS: STANDARD STUFF (possible to use –include-dependencies)

gem install capistrano
gem install mongrel
gem install mongrel_cluster
gem install gettext
gem install unicode

APACHE 2 with load balancer (for Mongrel cluster)

wget http://ftp.kddilabs.jp/infosystems/apache/httpd/httpd-2.2.4.tar.gz
tar zxvf httpd-2.2.4.tar.gz
cd httpd-2.2.4
./configure –enable-rewrite –enable-proxy –enable-load-balancer –enable-mods-shared=ALL
make
sudo make install

GHOSTSCRIPT & FONTS

# we need to install/update ghostscript and install ghostscript-fonts in order to get rid of the "`get_type_metrics': unable to read font `(null)' (Magick::ImageMagickError)" error:

wget http://jaist.dl.sourceforge.net/sourceforge/ghostscript/ghostscript-8.56.tar.gz
tar zxvf ghostscript-8.56.tar.gz
cd ghostscript-8.56
./configure
make
sudo make install

wget http://jaist.dl.sourceforge.net/sourceforge/gs-fonts/ghostscript-fonts-std-8.11.tar.gz
tar zxvf ghostscript-fonts-std-8.11.tar.gz
mv fonts /usr/local/share/ghostscript/8.56/

IMAGEMAGICK (required by RMagick)

wget ftp://ftp.u-aizu.ac.jp/pub/graphics/image/ImageMagick/imagemagick.org/ImageMagick-6.3.3-10.tar.gz
cd ImageMagick-6.3.3
./configure
make
sudo make install

RAILS: RMAGICK (manual install - I have always had multi-systems compatibility problems with the gem install)

wget http://files.rubyforge.mmmultiworks.com/rmagick/RMagick-1.15.4.tar.gz
tar zxvf RMagick-1.15.4.tar.gz
cd RMagick-1.15.4
./configure
make
sudo make install

MYSQL

wget http://ftp.iij.ad.jp/pub/db/mysql/Downloads/MySQL-5.0/mysql-5.0.37.tar.gz
tar zxvf mysql-5.0.37.tar.gz
cd mysql-5.0.37
./configure –with-extra-charsets=all –prefix=/usr/local/mysql
make
sudo make install

#in /etc/my.cnf
[mysqld]
set-variable = max_allowed_packet=32M # I upload large images into DBs, therefore need a bigger allowed packet size

(also need to change encodings to utf8)
default-character-set=utf8

SUBVERSION (required to capistrano deployment to work)

wget http://subversion.tigris.org/downloads/subversion-1.4.3.tar.gz
wget http://subversion.tigris.org/downloads/subversion-deps-1.4.3.tar.gz
tar zxvf subversion-1.4.3.tar.gz
tar zxvf subversion-deps-1.4.3.tar.gz
cd subversion-1.4.3
./configure
make
sudo make install

That all folks :) Hope this was helpful.