ActiveScaffold :: Wiki
Using the Model to Define Required Fields

Active Scaffold has a required field option. For example, you can enter the following in your controller:

config.columns[:col_name].required = true

And the following in your model:

validates_presence_of :col_name

First solution

Here’s how you can use the validations from your model to build these required fields automatically:

1. download and install the Validation_Reflection plugin. This plugin will help you pull out the validations that you’ve added to your model.

2. add a centralized way to pull out the “validates_presence_of” validations and apply them to the scaffold. Here’s a simple helper that I added to /lib:

module ActiveScaffoldHelper
  def add_required_columns(config, model)
    req_cols = model.reflect_on_all_validations
    if req_cols != nil
      req_cols.delete_if{|col| col.macro.to_s != 'validates_presence_of'}
      req_cols.each{|col| config.columns[col.name.to_s].required = true if config.columns.member?(col.name.to_s)}
    end
  end
end

(few questions from newbie:
Q1: in which exactly file should the above block be saved?
Q2: which /lib directory is meant? application’s or plugin’s one?

A. Save this code into :your_project/lib/active_scaffold_helper.rb.
)

3. Call the method in your controller:

include ActiveScaffoldHelper
class YourController < ApplicationController
  active_scaffold :your_model do |config|
    add_required_columns(config, YourModel)
  end
end

Make sure to place the add_required_columns call after any config.columns declaration

Second solution

1. As in the first solution, download and install the validation_reflection rails plugin.

2. Add the following code (might be to RAILS_ROOT/lib/active_scaffold_helper.rb , I myself have an active_scaffold_hacks plugin into which I throw all the additions and patches. Read this for more info: http://errtheblog.com/posts/67-evil-twin-plugin )

module ActiveScaffold::DataStructures
  class Column
    attr_writer :required_in_model
    def required_in_model?
      @required_in_model
    end

    def required?
      @required.nil? ? @required_in_model : @required
    end

    def initialize_with_required_in_model(*args)
      initialize_without_required_in_model(*args)
      self.required = nil # unknown value, use model to decide
      self.required_in_model = active_record_class.reflect_on_validations_for(name).
        map(&:macro).include?(:validates_presence_of)
    end
    alias_method_chain :initialize, :required_in_model
  end
end

3. Restart the application!

Now, if you defined a validate_presence_of validation on a field (in your model class), then this field will be defined required (watch the CSS classes of the field labels). No need to add code to the controller at all!

If you want to override this setting (to either way), just use the normal Active Scaffold syntax in your controller:

config.columns[:column_name].required = true   # or false

health insurance
auto insurance
insurance price quotes