Prototype your Rails application

Posted by Krzysztof Rączkiewicz Tue, 20 Mar 2007 12:26:00 GMT

Today I want to show you some piece of software that will help you quickly prototype your Rails application. We all know the scaffold mechanism. Without it there wouldn’t be famous Weblog in 15 minutes screencast. It’s really nice for beginning or if you what to quickly show someone (or to yourself) the idea of your application. But scaffold generate not very beautiful code. E.g.:
<% for column in User.content_columns %>
    <th><%= column.human_name %></th>
<% end %>
I’ll present you four tools that may look interesting for you: But first I’ll give you an idea how my sample app look. I will use one model (with one table associated of course). This is how migration file looks like:
create_table :users do |t|
  t.column :first_name, :string
  t.column :last_name, :string
  t.column :address, :text
end

Custom scaffold

You can obtain it from here and you install it just by unpacking it to YOUR_APP/vendor/plugins/ . After that you could check that you’ve got your new generator:
YOUR_APP$ ./script/generate
..
Installed Generators
  Plugins: custom_scaffold
..
Now all you need to do is to generate custom scaffold for your model (you don’t need to specify controller name, but it’s possible)
YOUR_APP$ ./script/generate custom_scaffold User admin
It’s generating much more prettier code,
<% for user in @users %>
  <tr>
    <td><%= user.first_name %></td>
    <td><%= user.last_name %></td>
    <td><%= user.address %></td>
    <td><%= link_to 'Show', :action => 'show', :id => user %></td>
    <td><%= link_to 'Edit', :action => 'edit', :id => user %></td>
    <td><%= link_to 'Delete', :action => 'destroy', :id => user %></td>
  </tr>
<% end %>
but it also have some problems. It generate code in YOUR_APP/app/views/admin/_form.rhtml with non existing method
<%= error_for 'user', 'first_name' %>
So you need to delete it or add this method definition to application helper, something like1 this

You can customize templates in YOUR_APP/vendor/plugins/custom_scaffold/generators/custom_scaffold/templates

MasterView

Authors call it template engine and you could get it from here. It is distributed as gem or plugin and installation is described on the main page. You can also watch screencast presenting MasterView power, so I will not focus on it.

Streamlined

This one is really nice and quite powerful. With help of this plugin you can generate nice, powerful including JavaScript interface for you ActiveRecord models. At the beginning, it was used mainly for administrative purpose, but there’s no contraindication to use it for general stuff.

You can watch nice screencast available at the framework homepage

Although it’s looks great, it also have few problems. First of all – lack of documentation and some times those doc’s are out of date. Also as you see it’s in early development so sometimes you could find a bug.

Ok. First create rails app, install plugin (version 0.0.7.1) configure your database then create model and controller:
dev$ rails streaml
dev$ cd streaml
dev$ ./script/plugin install http://svn.streamlinedframework.org/edge/streamlined
[CONFIGURE_DB]
streaml$ ./script/generate model User
streaml$ ./script/generate controller users
It would be available2 to specify model used by streamline, nevertheless pay attention to plural noun users right now. First of all add some code to application helper
module ApplicationHelper
  include StreamlinedHelper
end
Add code to your controller
class UsersController < ApplicationController
  layout 'streamlined'
  acts_as_streamlined
end
Now you could test your app. If you would like to use Ajax (editing, view etc.) you can meet problem in prototype library. You could find solution here There’s also generator available. You need to install older gem (e.g. streamlined_generator-0.0.6.gem from Download Streamlined section). After that you could generate “scaffold”
streaml$ ./script/generate streamlined user
and you’re able to make modifications of header, menu and layout generally in the stream/app/views/streamlined At the end I will show you quick UI configuration from the first example (version 0.0.7.1). Create file app/streamlined/user_ui.rb (if you need, create directory app/streamlined). Fill that file with:
module UserAdditions
end
class UserUI < Streamlined::UI
  user_columns :exclude => [:address]
end 
Refresh your view, and look at the difference. More examples here: Declarative View Options

Streamlined Edit in JavaScript window - Streamlined

AjaxScaffold

Last but not least Ajax Scaffold Generator. It is available as generator or dynamic plugin. I think all of you knows that soft and if you’ve never heard about it, there’s a bunch of good tutorial for newbies.

AjaxScaffold Edit - AjaxScaffold

1 Not exactly like this. You could see only an idea.

2 It would be available in 0.1.0 version

Tags , , , , , ,  | no comments

Comments

(leave url/email »)

   Preview comment