Article / January 23, 2008

Refreshing Rails Generated Views

By ELC/926 Views/1 Comment
It seems such a small thing, really, and it's so easy to take for granted. One of the things that I first came to love about Rails was the fact that it would generate standard views for your models. Sure, the HTML that the framework generates would charitably be described as skeletal; it requires a ton of work in order to look even remotely presentable. Even pushing design concerns to the side, you still have to account for the fact that all of your fields default to textboxes.

And yet, despite the headaches that accompany generated views, not having to type out every single field by hand is a beautiful thing. Of course, if that were all I had to say, I wouldn't have bothered to type this out. I mean, really, who'd want to read that?

So, I guess you're wondering why I called you all together, then. It's simple, really, I love generated views, but I also happen to prefer typing out my migration files rather than using command line arguments in the script/generate command. Unfortunately, there doesn't seem to be a native way to generate views from an existing migration, or, heaven forbid, a model that has been modified by multiple migrations.

In response to this dilemma, I decided to write a Rails Generator that would leverage the core view templates against field parameters that are created from an existing model.

And so, without further ado, I present the Refresh View plugin.

Install:

./script/plugin install http://refreshview.rubyforge.org/svn/tags/1.0/refresh_view/

Use:

Create your scaffold, modify the migration, then run the following command:

./script/generate refresh_view model_name

If you continue to make changes to the model's structure? Just keep refreshing the view. BUT, and this is important, the generator completely rewrites the view, so don't refresh the view if you've made changes you want to keep.

Happy Programming.

Comments

Posted by Dustin Davis on about 1 year ago19ab9b38d6cbbb42742a6e02c03b82cd?s=30

This seems to work OK, but not quite as expected.

Let's say I create a new rails app, then issue the following command:
ruby script/generate scaffold user

I have my scaffold view in app/views/users

If I do a migration to give the user table some actual data, then run your command:
ruby script/generate refresh_view user

It works, but it puts everything in app/views/user <-- missing s

If I try to run
ruby script/generate refresh_view users then it says the users model does not exist, so I have I have to do it the first way, then move the generated files to the users folder.

I'm am just learning ROR now, so maybe I'm missing something?

Add a Comment