Archive for the ‘rails’ Category

Creating Rails 1.2 Projects in a 2.0 World

Tuesday, February 17th, 2009

So maybe you’re trying to learn rails.

But maybe rails 2.0 just came out.  And maybe, in one fell swoop, every rails tutorial in existence just became obsolete.  So maybe you want to create a rails project using rails 1.2.6 for learning or compatibility reasons, or you just like to be retro.  Whatever.  You could change your RAILS_GEM_VERSION environment variable, but you don’t want all your new rails projects using old rails, just one.  So what do you do?

First, make sure you have rails 1.2.6 installed.  From the command line:

>gem list

You should see a list of gems, hopefully including

rails (2.0.2, 1.2.6, 1.2.3)

If you don’t have 1.2.6 already, install it this way (you’ll probably need to prepend sudo on the mac):

>gem install rails -v 1.2.6 –include-dependencies

Now, when you create the project from the command line, just type:

>rails _1.2.6_ myappname

And voila.  Blast from the past.

Script.aculo.us auto-complete and rails 2.0

Tuesday, February 17th, 2009

Scriptaculous’ basic autocompletion demo provides the following code that supposedly gets autocomplete up and running instantaneously:

# view
Just a little test:
<%= text_field_with_auto_complete :contact, :name %>

# controller
auto_complete_for :contact, :name
# Note: contact=object, name=field to match


Alas, it is not so. Not in rails 2.0, at least. Here are a few problems I ran into, and the ways I fixed them.


Problem:

undefined method 'auto_complete_for'

Solution: Rails 2.0 no longer comes with autocomplete standard.  Now you have to install it as a plugin for for your project (note: you’ll have to repeat this for any project using autocomplete).  In the root of your project directory, type:

>ruby script/plugin install auto_complete

Problem:

Ajax is not defined

Solution: Make sure you’re including the prototype.js javascript file from your public/javascripts folder.  Your html file should have the line:

<%= javascript_include_tag 'prototype' %>

Problem:

Ajax.autocomplete is not a constructor

Solution: You also need to include the scriptaculous.js file.  Unlike prototype.js, though, scriptaculous.js isn’t already in your javascripts folder.  It’s a good idea to make sure you have the latest scriptaculous release anyway, so I just downloaded the latest here and copied scriptaculous.js into my project’s public/javascripts folder.  Then I included it the same way that I included prototype.js:

<%= javascript_include_tag 'scriptaculous' %>

Problem:

Element.hasClassName is not a function

Solution:  This one’s a little more complicated.  Rails 2.0 now uses protect_from_forgery by default, assuring that only the appropriate user accesses or modifes data.  Unfortunately, this also bars autocomplete.  One way to get around this is to include this line in your controller:

protect_from_forgery :only => [:update, :delete, :create]

There are other, more secure, solutions to this problem, but this is what I’m using for now as I test.

Well, there you have it.  Once I made these changes, autocomplete worked like a charm for me.