Ruby on Rails


If you are using a plugin such as simple_captcha that requires RMagick, then you may encounter the following error during the rendering of jpeg catpcha image on Linux: (seen in the logs)

ArgumentError (unknown format: JPG):
/vendor/plugins/simple_captcha/lib/simple_captcha_image.rb:95:in `format=’

This mean that we need to install the delegate library for JPEG.
1. do apt-get install libjpeg62 (not sure if this is required or just dev is enough)
2. do apt-get install libjpeg62-dev
3. cd to where you downloaded Imagemagick and do ./configure, make, make install.
The result of ./configure will summarize the status of deletgate libraries in the end.

After this, a blank image should start to show up in place of the simple_catpcha image and the logs may show something like this:

sh: gs: command not found
sh: gs: command not found
RMagick: unable to read font `(null)’.
RMagick: Postscript delegate failed `/tmp/magick-XXfm5AzA’: No such file or directory.
sh: gs: command not found
sh: gs: command not found
RMagick: unable to read font `(null)’.
RMagick: Postscript delegate failed `/tmp/magick-XXg7z0RL’: No such file or directory.

4. Do apt-get install ghostscript if simple_captcha image shows up as a white blank image, without any letters. Don’t forget to do ./configure, make, make install from your Imagemagick source dir after this. Restarting of the server will be required for production environment.

Example result of ./configure status.

Delegate Configuration:
….
JPEG v1 –with-jpeg=yes yes
FreeType –with-freetype=yes yes

These 4 delegate libraries got simple_captcha working.
1. libfreetype6-dev (This also seems to be for fonts, but didn’t cut it for simple_captcha, but I had installed it already, so I let it be.)
2. libjpeg62-dev
3. libpng12-dev ( installed it just in case…)
4. ghostscript
Running ./configure, make, make install from imagemagick source dir is necessary after installing the above libraries.

If captcha image is not getting recognized, you may need to run the following (for rails > 2.0, there is a separate command for older Rails which can be seen from the simple_captcha website.)
rake simple_captcha:setup

then run
rake db:migrate

(These instructions are for Ubuntu for the error given below)
script/console
Loading development environment.
/usr/local/lib/ruby/1.8/irb/completion.rb:10:in `require’: no such file to load — readline (LoadError)
from /usr/local/lib/ruby/1.8/irb/completion.rb:10
from /usr/local/lib/ruby/1.8/irb/init.rb:252:in `require’
from /usr/local/lib/ruby/1.8/irb/init.rb:252:in `load_modules’
from /usr/local/lib/ruby/1.8/irb/init.rb:250:in `each’
from /usr/local/lib/ruby/1.8/irb/init.rb:250:in `load_modules’
from /usr/local/lib/ruby/1.8/irb/init.rb:21:in `setup’
from /usr/local/lib/ruby/1.8/irb.rb:54:in `start’
from /usr/local/bin/irb:13

1. apt-get install libreadline-ruby
2. apt-get install libreadline-dev
3. cd to where your downloaded source for ruby is located. (say ~/installed-software/ruby-1.8.6.tar.gz)
4. cd to ruby-1.8.6
5. run ./configure
6. run make
7. run make install
While running make, while the lines are streaming by, you will be able to notice ‘compiling readline’.
8. try ruby script/console from your app dir. It should load it this time.

  1. Creating table on mysql prompt : (separating partitions by 6 months each)

  2. In the migration file 001_create_stats.rb:

  3. After running rake db:migrate. On MySQL prompt, single partition being used in this example

  4. On MySQL prompt, two partitions being used in this example:

  5. On MySQL prompt, the default storage engine type is shown as InnoDB:

MySQL 5.1 at this time is in release candidate state, but I thought I’d just check it out for its partitioning feature.

Preparation

  1. Take a mysqldump of the existing databases. You don’t want to wipe out anything.
    backup_dir> mysqldump –all-databases > backup.sql -u root -p
    Enter password: ********
  2. Stop the InstantRails MySQL through the InstantRails Confirguration Interface (where Apache and MySQL server status shows?).
  3. Check using task manager, just in case the MySQL application is still running. No MySQL should be running. If you use any MySQL GUI tools such as MySQL Query Browser, close them too.

Installation of MySQL 5.1

  1. Downloaded mysql 5.1 setup exe (Windows ZIP/Setup.EXE (x86)) for Windows. I downloaded the 5.1.22 distribution.
  2. Cd to mysql-5.1.22-rc-win32 directory and click on setup.exe.
  3. This starts the MySQL setup wizard. Select ‘typical’ setup type and click Next, Next. This will start the installation in C:\Program Files\.
  4. The installation will additionally show another popup urging you to register, and that’s upto you. In our main installation window, the installation has completed. The ‘Configure the MySQL Server now’ box is checked, and the next step is to click ‘finish’. Click it.
  5. In the configuration popup, select Standard Configuration if you’re a chicken like me or select Detailed Configuration otherwise. Here we are going with Standard Configuration.
  6. The next step shows two options, ‘install as windows service’ and ‘include bin directory in windows path. I chose mySQL to be installed as a service. And checked the ‘include bin dir..’ box too. (I had encountered certain problems in starting up MySQL server on my Windows Vista when I did not choose to install it as a service.)
  7. The next step asks you to create a new root password, which you should do. I did not create an anonymous account, but just set the root password and moved on. I did not check the ‘allow remote access’.
  8. Click ‘execute’ in the next screen and voila! Your firewall software may pop up a window asking you to allow the security settings execution to go through.
  9. Open Task Manager -> Services. MySQL is already up and running as a service. You can configure it so that it does not start automatically. (manual start).
  10. Open a brand new command window (not the one using InstantRails ‘open a new console window’ but using the start menu. There’s a reason for it.) Key in mysql –version and it should show 5.1.xx. Key in the same in a Ruby console window got from InstantRails configuration and it should still show the previous verson of MySQL that your InstantRails is using.

Configure InstantRails to use MySQL 5.1

  1. Rename the mysql directory under InstantRails to say, mysql_old. (if you don’t want to delete it)
  2. I use a program called junction to create a symlink, a la Windows style.
  3. Key in:
    C:\Softwares\Junction>junction “C:\InstantRails\mysql” “C:\Program Files\MySQL\MySQL Server 5.1″
    Junction v1.05 – Windows junction creator and reparse point viewerCopyright (C) 2000-2007 Mark RussinovichSystems Internals – http://www.sysinternals.com
    Created: C:\InstantRails\mysqlTargetted at: C:\Program Files\MySQL\MySQL Server 5.1
    C:\Softwares\Junction>
  4. Now InstantRails’s mysql path points to your MySQL 5.1 installation. Keying in mysql –version in this window now should show the new version of mysql.
  5. Modify database.yml for the new root password if you chose differently from your previous root password.
  6. In the Ruby console window, just for convenience, cd to directory where your backup.sql. Now start a mysql shell here. We want to recreate the databases from our old copy.
    mysql -u root -p
    password: ********

    mysql> source backup.sql
    … this will show tons of row creation info etc… hang on…
    mysql> show databases;
    … this should show all your old databases…
    mysql> quit
  7. Start InstantRails. For me, the InstantRails configuration window shows MySQL as stopped, but we know it is running as a service.
  8. Running rake db:migrate is not necessary since we have already created the databases with all the data brought in from the old tables.
  1. cd C:/InstantRails/www. This is assuming your Instant Rails is installed in C:

  2. rails -d mysql myapp. This will create a new Ruby on Rails application in C:/InstantRails/www with MySQL as the default database server.
  3. On command prompt, key in: ruby script/server –port=3003. Check that you are able to access myapp at http://localhost:3003/ in your browser.
  4. Check that Apache is running by typing http://localhost/. This will display the index file fron C:/InstantRails/www directory.

  5. cd C:/InstantRails/apache/conf.
  6. Open http.conf file. In this file search for ‘VirtualHost’. This will lead you to the bunch of virtualhost declations for typo and cookbook applications.
  7. httpd.conf with InstantRails has proxy_module already uncommented for use. LoadModule proxy_module modules/mod_proxy.s.
  8. Add myapp block to httpd.conf:

  9. In the InstantRails configuration tool, select Configure -> Windows Hosts File. Click it open and add an entry to the end: 127.0.0.1 myapp. Save the file and exit notepad.
  10. Restarting Apache server was not needed since I was running this set up for the development environment.
  11. Key in http://myapp/ in the browser to check that your app is indeed showing up the index.html page from myapp/public folder.
  12. As a further adventure, add up on myapp:
    C:\InstantRails\www\myapp>ruby script/generate scaffold Blog blog_id:integer title:string body:text
  13. Run rake db:migrate
  14. Now key in http://myapp/blogs and it should show up the ‘index’ action for blogs correctly. Browse around by clicking on ‘new blog’ and see that the page is served as http://myapp/blogs/new.
  15. Also look at Apache access logs from InstantRails application and also the mongrel server logs from the command line.
  16. If keying in http://myapp/ takes you to http://www.myapp.com/, then you need to checks Windows Hosts file using InstantRails.
  17. If keying in http://myapp/ takes you to the Index of / directory listing that just shows the files in myapp folder, then you need to make sure you have given the DocumentRoot in the virtualhost declaration in httpd.conf as C:/InstantRails/www/myapp/public/ and NOT C:/InstantRails/www/myapp/ alone.
  18. Alternatives: in httpd.conf file if you
    (a) provide ServerName as http://www.myapp.com/,
    (b) provide ProxyPass / http://localhost:3003/ instead of ProxyPass /myapp/ http://localhost:3003/, and
    (c) include the entry 127.0.0.1 http://www.myapp.com/ in Windows hosts file,
    you can access you application at http://www.myapp.com/ from your desktop. If the browser continues to show the real http://www.myapp.com/ then just restart the browser or perhaps just clear the browser cache.
  19. It is not necessary to create your application under InstantRails/www. Having an application as InstantRails/rails_apps/myapp and providing the document root in httpd.conf as C:/InstantRails/rails_apps/myapp/public works just fine.

Installation of railsbench on Windows

  1. run from any directory (no download required) gem install hoe (this installs dependency gem hoe).
  2. Download railsbench gem from RubyForge and run gem install railsbench-0.9.2.gem from the directory where it is downloaded.
  3. Run gem list to see railsbench version 0.9.2 listed.

Running railsbench on Windows

  1. Set environment variable RAILS_ROOT through control panel to point to your app directory (here guitarati)
    RAILS_ROOT set to C:\InstantRails\rails_apps\guitarati
  2. Railsbench writes the result of tests in RAILS_PERF_DATA directory. Set environment variable RAILS_PERF_DATA through control panel. I chose to save the data in a newly created directory perf_data under RAILS_ROOT.
    RAILS_PERF_DATA set to C:\InstantRails\rails_apps\guitarati\perf_data
  3. Copy benchmarks.yml and benchmarking.rb from C:\InstantRails\ruby\lib\ruby\gems\1.8\gems\railsbench-0.9.2\config to RAILS_ROOT\config
  4. Copy benchmarking.rb from C:\InstantRails\ruby\lib\ruby\gems\1.8\gems\railsbench-0.9.2\config to RAILS_ROOT\config\environments
  5. run railsbench install from C:\InstantRails\ruby\lib\ruby\gems\1.8\gems\railsbench-0.9.2 directory. Should output .. creating database configuration: benchmarking
  6. Edit environment.rb to specify the plugins to be loaded:
    config.plugins = %W( simple_captcha acts_as_ferret …etc…). Without this, running railsbench perf_run gives undefined method errors for me.
  7. Run the simplest railsbench command
    C:\…\1.8\gems\railsbench-0.9.2>railsbench perf_run 100
  8. The above command prints something like this

  9. Understanding the railsbench results is a topic in itself to be covered in a later blog post.
  1. Suppose we decide to catch access to a forbidden part of our application by raising and rescuing AreaAccessDenied exception.
  2. For this, we can subclass StandardError to create a custom error that can be handled using rescue_action. Looks like only classes subclassing from StandardError get handled using rescue. Add the following line in a separate file or perhaps in application.rb if you don’t mind living with it.
    class AreaAccessDenied subclass StandardError; end
    (replace subclass with a ‘smaller than’ symbol before use)
  3. We need to handle those exceptions for which we want to show the user some output, in rescue_action_in_public.
  4. Edit config/development.rb to mark all requests non-local. This is necessary for rescue_action_in_public to catch exceptions in development mode. (for a clear idea, look at rescue_action code; all non-local requests are sent to rescue_action_in_public and others are sent to rescue_action_locally). (Do NOT FORGET to restart your development server for the environment settings to take effect!) config.action_controller.consider_all_requests_local = false #true
  5. Edit application.rb (Notice the double-colon prefix in ActionController in RoutingError and UnknownAction)
    def rescue_action_in_public(e)
    case e
    when ActiveRecord::RecordNotFound, ::ActionController::RoutingError, ::ActionController::UnknownAction
    logger.error “Exception: #{e.class}: #{e.message}” render :text => “Ooops!”
    when AreaAccessDenied
    logger.error “Exception: #{e.class}: #{e.message}” head :forbidden # status 403
    else logger.error “Exception: #{e.class}: #{e.message}” render :file => “#{RAILS_ROOT}/public/500.html”, :status => 500
    end
    end
  6. Using the above method alone doesn’t cut it in development mode. We also need to redefine local_request in application.rb to always return false. This is because rescue_action decides based on the return value of method local_request? whether to handle the exception using rescue_action_in_public or rescue_action_locally. controllers/application.rb
    def local_request?
    false
    end
  7. I did not see ‘uninitialized constant ActionWebService::Dispatcher::ActionController::RoutingError’. (what are you talking about?). But routing errors were not getting caught through my rescue_action_in_public method without a double-colon.

If you are using RESTful authentication plugin AND simple_captcha for user signup AND using @object.save_with_captcha AND ALWAYS encountering “Captcha image did not match with text”, then:

adding :captcha and :captcha_key to :user_accessible in user.rb may solve the problem. If this is the reason why your catpcha input is always getting rejected, a warning shows up in the logs saying attributes captcha and captcha_key are not accessible or something..

  1. The RMagick README asks you to update your Rubygems version to at least 0.9.4. The latest available version at this time is 0.9.5. But people have reported problems with RMagick installation using 0.9.5 version of RubyGems.
  2. I faced the same problem when I tried to install RMagick after updating my RubyGems version to 0.9.5. So I decided to install RubyGems 0.9.4 to be on the safe side and try insalling RMagick again. This was the status of installation of RMagick using RubyGems 0.9.5 “Successfully installed rmagick-1.15.9-unknown 1 gem installed.”
  3. Download rubygems-0.9.4.zip from RubyForge Ruby Gems. I downloaded it to C:\Softwares.
  4. Unzip rubygems-0.9.4.zip.
  5. cd to C:\Softwares\rubygems-0.9.4\rubygems-0.9.4. You should be able to see a file named setup.rb in this folder. On command prompt cd to this directory and key in: ruby setup.rb
  6. This installs 0.9.4 version of RubyGems.
  7. To double check the correct version of Ruby Gems key in (from any directory): gem –version and that should print 0.9.4
  8. We need to use the rmagick-win32 gem for installing RMagick on Windows. This gem is bundled with a a copy of the ImageMagick Windows installer so we don’t need to download our own copy.
  9. Download the rmagick-win32 gem from the RMagick project page on RubyForge and unzip it into a temporary directory.
  10. I downloaded RMagick-1.15.9_IM-6.3.5-8-Q8_prod.zip from RubyForge RMagick and saved it in C:\Softwares on my desktop.
  11. Unzipped RMagick-1.15.9_IM-6.3.5-8-Q8_prod.zip
  12. Unzipped RMagick-1.15.9.tar.zip that was inside RMagick-1.15.9_IM-6.3.5-8-Q8_prod
  13. Clicked on ImageMagick-6.3.5-8-Q8-windows-dll to install ImageMagick. This installs ImageMagick but I am not sure if we are supposed to install this manually or whether gem install rmagick will do it for us. Nonetheless, since I am a chicken, I installed it myself. This installer does not seem to interfere with an existing, different version of ImageMagick.
  14. To see the version of ImageMagick installed, key in: convert –version
  15. Now install the rmagick gem. C:\Softwares\RMagick-1.15.9_IM-6.3.5-8-Q8_prod> gem install rmagick-1.15.9-win32.gem –local This prints ‘Successfully installed rmagick, version 1.15.9′.
  16. I did not need to uninstall Ruby and install it through the one click installer. (What are you talking about?)
  17. Now restart your development server. ruby script/server
  18. There is no need to ‘require RMagick’ in your environment.rb or source code.
  19. Errors after installation of RMagick, on restarting the development server.
    “warning: already initialized constant OPTIONS > … ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/mongrel.rb:18:> undefined method `options’ for []:Array (NoMethodError)” This seems to be because of an error in RMagick installation. Key in: gem list on the command prompt to see if rmagick-win32 is indeed showing in the list. If not, you need to install RMagick.
  20. “Procedure entry point HSL transform could not be located in the dynamic link library CORE_RL_magick.dll” OR “CORE_RL_magick.dll not found”. For CORE_RL_magick.dll problems you can do 2 things:
    check if ImageMagick is correctly installed. If there are any other installations of ImageMagick installed, it might be better to uninstall them.Restart your Windows machine. Yes, not the development server, but your computer. This makes available all the paths to ImageMagick. This tip, courtesy: http://www.texxors.com/?p=24. To make sure if this is indeed the problem, key in: convert –version on a path not in ImageMagick\bin folder (where convert.exe) resides. If it complains of invalid drive etc. you know that the path to ImageMagick is not being recognized. Restarting the machine helps correct this problem. After restarting, if you key in: convert –version, you should be able to see: Version: ImageMagick 6.3.5 09/01/07 Q8 http://www.imagemagick.orgcopyright/: Copyright (C) 1999-2007 ImageMagick Studio LLC

Subversion installation

  1. Go to Subversion project page. This page lists the subversion packages for various platforms. Scroll down to the Windows area.
  2. Select the Apache server version. I select ‘Win32 packages built against Apache 2.2′. If you are not sure or do not find your version of Apache listed, selecting Apache 2.2 may still do.
  3. This will take you to the list of downloads. Scroll right to the end and save
    svn-win32-1.4.5.zip. This is Subversion 1.4.5 Win32 binaries for Apache 2.2.x. You can select whichever version suits your needs. This is the one I selected.
  4. On your PC, unzip this folder. This will create a folder structure like so (if you unzip it in C:\Softwares): C:\Softwares\svn-win32-1.4.5\svn-win32-1.4.5
  5. Append the path of the Subversion bin directory (where svn, subversion, svnlook etc. reside), C:\Softwares\svn-win32-1.4.5\svn-win32-1.4.5\bin to your Control Panel -> System -> Advanced System Settings -> Advanced -> Environement Variables -> System Variables -> PATH.
  6. Now open a brand new command prompt and type svn. If it does not throw an unknown program error, hurray! You’ve installed Subversion successfully.

Ferret installation

  1. For downloading the latest stable version of Ferret gem go to Ruby Forge Ferret page. From here, select ferret-0.11.5-mswin32.gem or whichever win32 version shows topmost.
  2. Suppose you downloaded the gem file to your C:\Softwares directory. You can install ferret by keying in gem install ferret.
    C:\Softwares>gem install ferret
    Successfully installed ferret, version 0.11.5
    Installing ri documentation for ferret-0.11.5-mswin32…
    Installing RDoc documentation for ferret-0.11.5-mswin32…
    C:\Softwares
  3. That’s it. You’re done

acts_as_ferret installation

  1. To install it in your Rails application say, test_app, change directory to your app. Then key in the following on the command prompt to install the latest stable version of acts_as_ferret.
    C:\InstantRails\rails_apps\test_app> ruby script/plugin install svn://projects.jkraemer.net/acts_as_ferret/tags/stable/acts_as_ferret
  2. If this command ends up throwing error or just returns without any status information, check the URL and check whether svn is indeed included in your PATH.
  3. If everything goes OK, this installation will spit out lots of information about the installation as it goes.
  4. RESTART the development web server (mongrel/webrick) to be able to use acts_as_ferret in your application.
  5. Congrats! You can now go ahead and use acts_as_ferret in your application. If the view code gives the error ‘Unknown class acts_as_ferret in class …’, you probably forgot to restart the development web server.

acts_as_ferret resources

  1. Acts_as_ferret Wiki
  2. Using the acts_as_ferret DRb server (Here is the answer to: what the $@#* is a DRb server?)
  3. A splendid acts_as_ferret tutorial

Next Page »

Follow

Get every new post delivered to your Inbox.