Friday, November 30, 2007

RESTful authentication plus simple_captcha (Captcha image did not match with text)

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..


Monday, November 26, 2007

Step by step RMagick installation for Windows


  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

Monday, November 19, 2007

Step by step subversion (svn), Ferret and acts_as_ferret installation on Windows

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

Thursday, November 15, 2007

RESTful routing for a singleton resource (singular RESTful routes)

The generated RESTful routes are by default pluralized. So we get routes like /topics/12/posts/4. But what about the routes for resources that are singleton? A user can have a single identity object describing his first name, last name, location etc. We'd like to be able to refer to identities in singular within the context of the logged in user. It would be idiomatically better if the route were /identity/new. In this case map.resource comes to the rescue.

1. On the command line, key in:

ruby script/generate scaffold_resource Identity

2. In config/routes.rb key in:

# map.resources :identities <= comment out automatically added mapping map.resource :identity, :controller => "identities"

3. In the views code, change URLs from identity_path(@identity) etc. to identity_path. E.g. in new.rhtml change the form :url to identity_path from identity_path(@identity)

4. In the controller, which is still called IdentitiesController, "index" action can be removed now since we're only supposed to have one such resource per user. Since we are not passing params[:id] anymore, we need a way to get hold of the Identity object that corresponds to the currently logged in user. This can be done using something like:

@identity = @logged_in_user.identity

assuming @logged_in_user is the currently logged in user and 'has_one' Identity. The idea is to have a means to get the identity object.

5. That's it. You can access URLs such as

http://localhost:3000/identity/new and http://localhost:3000/identity;edit

instead of
http://locahost:3000/identities/new and http://localhost:3000/identities/12;edit

More information on singleton resources can be found in the Rails API documentation. Looking closely at the URLs described in the controllers for map.resource and map.resources in the API documentation clarifies point#3 about removing @identity, that is, params[:id].

RESTful Rails: Anorexic Controllers and Fat Models

It is scary how thin and lean Rails controllers become after you RESTify them (shudder). I love refactoring, but this morning, as I sat down for my morning ritual of refactoring previous day's code, I figured I couldn't! There just wasn't any extra sliver of code to be mercilessly chopped off from our bright and shiny RESTful controllers. Background: our application Guitarati is being created completely using Ruby on Rails and this week, I put our current code to REST.

I am going through a love-hate relationship with REST. My emotions are swinging wildly for and against this pithy thing. May be because I am commitment-phobic. May be I don't want to tie myself down to a design philosophy just yet. May be because I've not seen 30 summers in my life. Sometimes, REST feels like a strait-jacketed approach that curbs creative design (try Ajax + nested routes). And at other times, it feels like a God-send to bring order to the Universe (just look at your controller code).

Nonetheless, here are some resources for those aiming for a RESTful lifestyle. (better sleep, less worries and more cash)


2. Taming the RESTful BEAST (a whole working RESTful application from the good guys). And a nice diagram explaining the app.


4. Those with some extra cash can pay $9 and watch the RESTful show. Mind you, this talks of REST before Rails 1.2 came out, so I'm not sure if this will be up-to-date vis-a-vis Rails 2.0 preview.

PS: the loving-REST personality of mine wanted to title the post as 'RESTful Rails: Lean Controllers and Mean Models', but you know who won in the end. For the puritans: I despise anorexia as much as you guys do and I have nothing against fat people.