Saturday, December 26, 2009

Recipe 1: In-Place Form Editing

(This post is one in a series of updates Chad Fowler's Rails Recipes.)

What is it. A field can be used for both display and editing. It initially appears normally. When clicked, it becomes an edit field.

How. This is done through a Scriptaculous control. The recipe describes how to get Rails to use it.

Main modification. One reason this recipe no longer works is that much of what used to be in Rails 1.0 became a plugin for Rails 2.0. So the rails-in_place_editing plugin is required.

Here are the other differences between the book and current Rails behavior:


  1. The call to script/generate should now include column names, e.g.

    ruby script/generate scaffold Contacts name:string email:string phone:string

  2. The forms built by scaffolding are now different. The excerpts in the book will snippet no longer reflect what scaffolding creates. You won't find this in show.html.erb:
    <% for column in Contact.content_columns %>
    <p>
    <b><= column.human_name %> <= h @contact.send(column.name) %>
    </p>
    <% end %>
    One reason you don't see this is that the loop above would cause the timestamp fields that Rails creates by default to appear with along with name and address. Generally, we do not want the user editing those fields. Instead the loop is now unrolled. You'll see elements like this for phone:
    <p>
    <b>Phone:</b>
    <%=h @contact.phone %>
    </p>

No comments:

Post a Comment