Wednesday, December 30, 2009

Recipe 2: In place editing with drop-down

(This post is one of a series that updates Chad Fowler's Rails Recipes for Rails 2.x.)

What. Like the first recipe, this one provides in-place editing. It is in-place editing of a country field. Instead of a text field, the editor control is a drop down list of countries.

How. The in-place editor plugin is extended to add this functionality.

Anachronisms.

  1. In place editing was moved out of Rails with version 2, so you will need the in placed editor plugin.

  2. The country list also moved out. Unfortunately, lists of countries are not without controversy, e.g., is Taiwan a country or a province? So there are multiple plugins that provide lists of countries.
  3. Some changes to the Scriptaculous interface require that another option be passed to Ajax.InPlaceSelectEditor.

Modifications.

1. The method in app/helpers/application_helper.rb should look like this:

  def in_place_select_editor(field_id, options = {})
js_options = {}
# Necessary for the submit to work
js_options['callback'] = "function(form) { return #{options[:with]} }" if options[:with]
# Necessary for the options to show up
js_options['selectOptionsHTML'] = %('#{escape_javascript(options[:select_options].gsub(/\n/, ""))}')

function = "new Ajax.InPlaceSelectEditor("
function << "'#{field_id}', "
function << "'#{url_for(options[:url])}'"
function << (', ' + options_for_javascript(js_options)) unless js_options.empty?
function << ')'

javascript_tag(function)
end

2. The following line must be added to the controller:
in_place_edit_for :contact, :country

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>

Rails Recipes by Chad Fowler

Chad Fowler's book Rails Recipes is both useful and outdated. Built on Rails 1.0, few of its recipes work verbatim. My goal in this first series of posts is to update his recipes so that others can use them.