Skip to content
cainlevy edited this page Sep 14, 2010 · 14 revisions

Overview

The nested feature set allows you to easily edit relational data for your models (See example below) all on the same page.

The :nested action MUST be included if you are using nested scaffolds (it is included by default). If you are limiting the actions available using config.actions=, you must include :nested


config.actions = [:list, :nested]

Nested links can be disabled by removing the action as follows:


config.actions.exclude :nested

add_link local

Lets you add an Action Link configured to open a set of nested scaffolds for the given associations. You should specify association names as they are found in the config.columns collection, not model names or table names.

Example:

Create an action link to open all “contacts” for a row


  1. app/models/company.rb
    class Company < ActiveRecord::Base
    has_many :contacts
    end
  1. app/models/contact.rb
    class Contact < ActiveRecord::Base
    belongs_to :company
    end
  1. app/controllers/contacts_controller.rb
    class ContactsController < ApplicationController
    active_scaffold :contacts do | config |
    end
    end
  1. app/controllers/companies_controller.rb
    class CompaniesController < ApplicationController
    active_scaffold :companies do |config|
    config.nested.add_link(“Company’s contacts”, [:contacts])
    end
    end


Figure 1.0: A nested scaffold for the above example

You can also open many nested scaffolds at a time with one link:


config.nested.add_link("Show contacts/projects", [:contacts, :projects])
  1. Assuming Company has_many :contacts and has_many :projects
  2. Also, assuming that ProjectsController exists and is an active_scaffold for :projects

shallow_delete v1.1 global local

Default value is false. If set to true, the delete action in a habtm association is replaced with delete existing.

Example:


config.nested.shallow_delete = true

Other methods of adding nested scaffolds

Attach a link to a column:


columns[:contacts].set_link('nested', :parameters => {:associations => :contacts})

Attach a link to a column that will open up many nested scaffolds at time


columns[:contacts].set_link('nested', :parameters => {:associations => "contacts projects"})

Configuring the Nested Scaffold

ActiveScaffold tries to find the configuration for the nested scaffold by searching for a controller named in a conventional #{model_name}Controller fashion. So for example, if a Company has_many :contacts, then when CompaniesController tries to nest ContactsController it will try to take the configuration from a ContactsController. Failing that, it will use a default configuration.

If the default behavior for finding configuration from a conventional controller doesn’t work for you, you may override the active_scaffold_controller_for method and define your own search rules. See API: Core for more information.

Reverse Associations

Nested scaffolds depend on something we call the “reverse association”. Associations are technically uni-directional, but in practice they are bi-directional. That is, when you set up an association like Contact :belongs_to :company, you’ve only set up one direction. In order to make it bi-directional you must also say that Company :has_many :contacts. In this setup, the reverse association for Company#contacts is :company, and the reverse association for Contact#company is :contacts.

ActiveScaffold does its best to find the reverse association. If for some reason this best effort attempt fails, you may specify the reverse association by editing the column object like in the example below. Note that the example below is not strictly necessary – ActiveScaffold should find reverse associations even if the name is slightly unconventional.


active_scaffold :companies do |config|
  config.columns[:contacts].association.reverse = :company
end

WARNING: if you are modifying the @config.columns collection, make sure the association column is present on both sides of the association. Any column used by ActiveScaffold, in this case the association columns, needs to exist in this collection.

Clone this wiki locally