-
Notifications
You must be signed in to change notification settings - Fork 330
Getting Started
(See Old versions at bottom for Rails 3.0 and 2.3)
Use the gem
gem install active_scaffold
Add it to your Gemfile of course:
gem 'active_scaffold'
Then, per Sergio’s post you need to add
= require active_scaffold
to your assets pipeline, by adding it as a comment at the bottom of the following two ‘manifests’ files: app/assets/javascripts/application.js and app/assets/stylesheets/application.css (the former with // in front and the latter with * in front).
NOTE: Rails 3.1 can use render_component for embedded scaffolds, but it’s not needed and it’s untested. You can install vhochstein’s gem:
gem install render_component_vho
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, and active_scaffold routes:
rails g active_scaffold Model attr1:type attr2:type …
That’s it! Your first ActiveScaffold is up and running. But if you crave more, read on!
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.set_defaults do |config|
config.ignore_columns.add [:created_at, :updated_at, :lock_version]
end
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!
For rails 3.0 use the gems version 3.0.23 and for rails 2.3.x rails use the second command (or third one if you need backwards compatibility)
gem install active_scaffold -v 3.0.23
./script/plugin install git://github.com/activescaffold/active_scaffold.git -r v2.4
./script/plugin install git://github.com/activescaffold/active_scaffold.git -r rails-2.3
Rails 2.3.x needs the plugin render_component for nested and embedded scaffolds
./script/plugin install git://github.com/ewildgoose/render_component.git -r rails-2.3
In rails 3.0 use rails g active_scaffold_setup
(or rails g active_scaffold_setup jquery
to use jquery instead of prototype) and it will download required js files to your public/javascript directory, update application layout and put a initializer to enable jquery in last case.
In rails 2.3.x use script/generate resource
and create the layout by yourself, or use script/generate scaffold
and remove generated controller code and the views.
Add this inside your layout:
<%= javascript_include_tag :defaults %>
<%= active_scaffold_includes %>
Add this inside the controller you want to run the scaffold:
active_scaffold :<your_model_name>
Add :active_scaffold => true to your routes.rb file:
map.resources :<your_model_name>, :active_scaffold => true
In rails 3.0 you use as_routes in your routes.rb file instead:
resources :users do
as_routes
end