public
Description:
Home | Edit | New

Chaining Form Fields

Sometimes you want update a form field after set a value in other field, for example, you have a form with a dropdown to select an author and another dropdown to select a book, and you want select books from the chosen author. So you need to render the books dropdown each time an author is chosen.

class UsersController < ApplicationController
  active_scaffold do |config|
    config.columns[:author].form_ui = :select
    config.columns[:author].options = {:update_column => :book} # enables the "magic"
    config.columns[:book].form_ui = :select
  end
end

class UsersHelper
  def options_for_association_conditions(association)
    if association.name == :book
      {'books.author_id' => @record.author_id}
    else
      super
    end
  end
end

The helper code is to show only books belonging to chosen author, like is explained in Custom Association Options.
It doesn’t work only with association columns, it work with simple columns too, and with your form overrides

Last edited by scambra, Tue Apr 28 08:20:27 -0700 2009
Home | Edit | New
Versions: