Gotcha: Defining a controller action called 'process'

Posted by Hosh Thu, 23 Aug 2007 09:48:00 GMT

Early in the morning on #rubyonrails, someone came into channel asking for help. He said that, inexplicably, on controller out of his several controllers stopped functioning. When asked for a log, he gave this pastie.

This is very puzzling. I even looked at the code around line 330 in ActionController:base

327 class << self
328 # Factory for the standard create, process loop where the controller is discarded after processing.
329 def process(request, response) #:nodoc:
330 new.process(request, response)
331 end

When asked about what his controller looked like, he gave this pastie.

Can you spot the error?

The Gotcha

As commented in ActionController::base, a new instance of the controller is generated and the method process is called. Unfortunately, all actions are also method definitions. By attempting to define an action called 'process', it overwrote the original process. That explained the error with the "Wrong number of arguments."

The Judgment

Don't name your action 'process'. And more importantly, dig into the code when you get weird errors like this.

-Hosh

Rails BugTracker

Posted by Hosh Wed, 15 Aug 2007 15:24:00 GMT

I just cracked open a Rails Bug Tracker. The screenshots look good, and the project looks mature. I've been shopping for a native Rails bug tracker for use with our company, Isshen, LLC. This project looks like something I can quickly install and start using, despite its status as "Beta" on Rubyforge.

Suspicion first filled me when I saw that the project had a "Win32 source" download. That does not make any sense. A Rails app is fairly OS-agnostic. The download is distributed in a zip file. When opened, the source itself is packaged as a RAR. And there was a database.sql schema.

The schema was bad. It looked like something I would have written before I encountered Rails. There were three migration scripts in the Rails source. There was one that says "create_object". Huh?

Curious, I opened up app/model. No dice. There were three models ... and a file called mysqlconnection.rb. Why would a Rails app need that for? I got real suspicious.

And this suspicion was confirmed when I looked at the controllers. Each controller action would create a new mysqlconnection, generate the SQL query on the fly, and then render the template.

Essentially, the Rails Bug Tracker is a superficial port of a PHP program. And that PHP program wasn't even written using MVC architecture.

Wow.

Useful tricks with script/console

Posted by Hosh Wed, 01 Aug 2007 11:43:00 GMT

One of my favorite tools for Rails is script/console. It lets me test out snippets of code to make sure things work the way they are supposed to work. I found two useful things this morning:

Tab completion

Tab completion works in script/console.

script/console
user = User.find :first
user.up[tab]

You have no idea how happy that makes me.

reload!

After I found out about tab completion, I found myself making some heavy code changes, and testing them out in the console. I kept having to quit and restart the console. Out on a lark, I hit 'r' and hit tab ... and found 'reload!'. I always wanted that command, but I had thought it was 'reload' not 'reload!'. Go figure.

Rails Plugins

Posted by Hosh Sat, 28 Jul 2007 19:51:00 GMT

This was brought up at #rubyonrails -- someone's idea of the top 11 Rails plugins.

There are a few missing from there:

  • Technoweenie (Rick Olsen)'s acts_as_authenticated and restful_authentication. These plugins do not tell you how to authenticate anything. Rather, you install the plugin then generate authentication code. From there, you modify it as you see fit.

  • Technoweenie also wrote attachment_fu

  • Rather than acts_as_taggable, I use acts_as_taggable_on_steroids, which is based on acts_as_taggable. Neither supports multiple tags per model, and the acts_as_taggable_on_steroids author invited me to write a patch, should I write one.

The largest repository of Rails plugins I've found is located at http://agilewebdevelopment.com/plugins.

Ruby beautifier

Posted by Hosh Thu, 26 Jul 2007 07:11:00 GMT

I use jEdit for Ruby on Rails development. I only need three things from it:

  1. Project Viewer / File tree panel (like on TextMate)
  2. Search in directory. The "find" command is OK, but the jEdit HyperSearch will take me directly to the line number in the file.
  3. Autoformatting and autoindenting.

I am not particular to syntax highlighting. I don't need to have integrated SQL, log windows, or Subversion/SVK integration -- I use Tilda, a drop-down Gnome terminal bound to function keys.

jEdit 4.2final does (1) and (2) well. It does not do (3) well. I may get fed up enough to find a new editor soon to work with Ruby on Rails. Now, I can work with the broken autocompletions, but I have difficulty working with the broken autoindents. It looks fine when I see it in jEdit, but the resulting text does not output correctly. Probably because I did not set the indent options properly. Update: Yep, need to set "soft indent" and emulate indents with spaces.

Fortunately, there is a command-line script that will beautify Ruby scripts. It may not work in all the weird cases, but I can bind it to a rake task and apply it to the entire app/ directory.