-
Notifications
You must be signed in to change notification settings - Fork 330
Getting Started
To get started with a new Rails project
Added to Gemfile
gem 'active_scaffold'
Add jQuery, by adding jquery-rails gem or other ways, active_scaffold:install
generator will add the lines to load jQuery if jquery-rails gem is available, if using other ways, you must setup to ensure jQuery is loaded before active_scaffold.
Run the following commands:
bundle install
rails g active_scaffold:install
rails db:create
rails g active_scaffold:resource User name:string
rails db:migrate
Run the app and visit localhost:3000/users
It has some notes to install on a mac, for rails 6, but some tips may apply to newer versions.
For Rails < 6.1, see older versions)
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:resource Model attr1:type attr2:type
rails 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!