-
Notifications
You must be signed in to change notification settings - Fork 330
Getting Started
(For Rails 3.0 and 2.3 see older versions)
Use the gem
gem install active_scaffold
Add it to your Gemfile of course:
gem 'active_scaffold'
Then you need to add active_scaffold to your assets pipeline, by adding it as a comment at the bottom of the following two ‘manifests’ files:
in Rails.root/app/assets/stylesheets/application.css (or application.scss)
*= require active_scaffold
in Rails.root/app/assets/javascripts/application.js
//= require active_scaffold
Finally, install the routes:
rails g active_scaffold:install
Use active_scaffold
or active_scaffold_controller
generators instead of resource or controller generators in rails 3. They will create controllers with active_scaffold enabled, as well as active_scaffold routes:
rails g active_scaffold Model attr1:type attr2:type
rake db:migrate
Note: On rails 4+ you should use
rails g active_scaffold:resource Model attr1:type attr2:type
rake db:migrate
That’s it! Your first ActiveScaffold is up and running.
Your scaffold should be up and running with default everything right now. Not quite how you want it?
First let’s introduce the global config block. You probably noticed that the active_scaffolding includes everything in the table. Let’s remove a few of these columns with one easy config block. Create an active_scaffold.rb
initializer in config/initializers
. And don’t forget to restart your application whenever you make changes to this file.
# config/initializers/active_scaffold.rb
ActiveScaffold.defaults do |config|
config.ignore_columns.add [:created_at, :updated_at, :lock_version]
end
It’s recommended to call `clear_helpers` in ApplicationController, as some helpers defined by ActiveScaffold, such as active_scaffold_enum_options, options_for_association_conditions, association_klass_scoped, are usually overrided for different controllers, and it may cause issues when all helper modules are available to every controller, specially when models have associations or columns with the same name but need different code for those overrided helper methods.
class ApplicationController < ActionController::Base
clear_helpers
Let’s have a look at the local config block. This block goes in the model’s corresponding controller. The config block for the Company model goes in the CompaniesController. ActiveScaffold restricts one model per controller. Now for an example:
class CompaniesController < ApplicationController
active_scaffold :company do |config|
config.label = "Customers"
config.columns = [:name, :phone, :company_type, :comments]
list.columns.exclude :comments
list.sorting = {:name => 'ASC'}
columns[:phone].label = "Phone #"
columns[:phone].description = "(Format: ###-###-####)"
end
end
ActiveScaffold tries to be flexible: change the labels, decide which columns to include, control the columns included per-action, define a default sort order, specify a column label and a column description. Check the API docs to see what’s possible!