ActiveScaffold and ‘helper :all’

On a recent project we have been using ActiveScaffold in an effort to learn the tool and judge it’s usefulness. It also seemed like a really good fit for the CRUD application we are working on.

ActiveScaffold is interesting in that it basically makes a best effort guess as to what should be Shown, Listed, Editable etc via the db schema and ActiveRecord relationships. As a result, any application that has a modicum of complexity needs to have the default scaffolding overridden.

That’s fine, they provide a nice way of doing that. For example, let’s say we have the object User with it’s corresponding Model and Controller. When you navigate to domain.tld/user/ We get a nice list of the users table from the database. However if it’s like most applications and Users belong_to something, in that column it will likely show you a foreign key to another table. Not really very useful. It is however easily fixed.

If you have your ActiveRecord relationships set up correctly, in your user_helper.rb you can do something like:

def company_id_column(record)
record.company.name
end

Now, instead of displaying the foreign key, you’ll get the value of the name column from the company table. Awesome right?

In Rails 2.0 it appears that in the application controller, they now include the following line:

helper :all # include all helpers, all the time

Basically it takes all the methods from all the XXXXX_helper.rb files and mixes them in. This caused me a few minutes of head scratching when I was moving on to a second object, and when I loaded up the scaffold, some of the functionality was already there. And that’s great, except some of the overrides aren’t exactly the same, though they are named the same since it favors convention over configuration.

So just comment out ‘helper :all’. Any helper methods you want to share, toss those in application_helper.rb, the rest, leave in the individual controllers where they belong.

Collide, no more.

About Nathan Powell

I am a middle aged technologist freak-ball.
This entry was posted in programming. Bookmark the permalink.

Comments are closed.