undefined method `render_component’ with ActiveScaffold and Rails 2.3.2

Problem
When using a nested (or embedded) scaffold in ActiveScaffold with Rails 2.3.2 you have the error:

undefined method `render_component'

Solution
According to the issue here, in Rails 2.3 the render_component has been removed.

Install the render_component from:

script/plugin install git://github.com/lackac/render_component.git -r rails-edge

and restart your server, and it should be working.

Using ActiveScaffold in Rails 2.2 after using default scaffolding

Problem
You want to use ActiveScaffold in Rails 2.2, in a model that you have created using the standard Rails scaffolding script.

Solution

  1. Install the ActiveScaffold plugin:
    script/plugin install git://github.com/activescaffold/active_scaffold.git -r rails-2.2
  2. In your layout (model or application for all models) add the following:
    <%= javascript_include_tag :defaults %>
    <%= active_scaffold_includes %>
  3. In your controller file delete all the standard scaffolding code and add one line, so that your new controller should look like:
    class SomethingsController < ApplicationController
      active_scaffold :something
    end
  4. To configure a RESTful scaffold add the following to your route.rb file:
    map.resources :somethings, :active_scaffold => true
  5. Delete the views that were created from the standard rails scaffolding in the views/somethings folder (edit, show, index ...)
  6. Restart your server

You should now have an active scaffold for your model.

Adding icons to ActiveScaffold’s actions

Problem
You want to replace the standard text descriptions in ActiveScaffold’s actions with your icons.

Solution

  1. Create a file called active_scaffold_overrides.css in ror_project/public/stylesheets
  2. Copy the css code from ror_project/vendor/plugins/active_scaffold/frontends/default/stylesheets/stylesheet.css and paste it in to the file created in the previous step.
  3. Create a directory my_images in ror_project/public/images/activescaffold to hold your images.
  4. Copy to the new directory the images you want to use in the application.
  5. In the active_scaffold_overrides.css file find the section named:
    /* Table :: Actions (Edit,Delete) .... */

    and for each action that you want to use an icon add (ie edit)

    .active_scaffold tr.record td.actions a.edit {
        display:     block;
        width:        16px;
        height:       16px;
        background: transparent url(../../../images/active_scaffold/my_images/image.png) no-repeat right 50%;
    }
  6. In the application.rb, if you want the links to be the same for the whole application add:
    ActiveScaffold.set_defaults do |config|
        config.update.link.label =""
    end
  7. in your layout/application.rhtml (create one if you don’t already have one), include
    <%= stylesheet_link_tag "active_scaffold_overrides" %>
  8. Restart webrick/mongrel

Controller testing in Active Scaffold

Problem

You need to have to functionally test your controller when you are using ActiveScaffold. There are pieces of code that tell you how to do that on a normal ror application (ie without ActiveScaffold), like recipe 7.17 on the Rails Cookbook, or a fragment of code in an Active Scaffold application, but they don’t have the full details.

Solution

Here is an attempt to give some more detailed step by step instructions on how to do it.

  1. The migration for the example customer model.
  2. class CreateCustomers < ActiveRecord::Migration
      def self.up
        # Create customers table
        create_table  :customers do |t|
          t.column  :customer_code, :string, :null => false
          t.column  :created_at, :datetime
          t.column  :updated_at, :datetime
        end
      end
    end
  3. The customer model, with the extra function to display the customer code
  4. class Customer < ActiveRecord::Base
    
      has_many    :numbers
    
      def to_label
        "#{customer_code}"
      end
    end
  5. The customer controller app/controllers/customers_controller.rb, using the Active Scaffold
  6. class CustomersController < ApplicationController
    
      active_scaffold :customer do |config|
        config.list.columns = [:customer_code, :numbers, :created_at, :updated_at]
        config.columns[:customer_code].inplace_edit = true
     end
    
    end
  7. The fixtures YML file test/fixtures/customers.yml
  8. first_customer:
      id:               1
      customer_code:    123
      created_at:       2007-09-26 11:17:07
      updated_at:       2007-09-26 11:17:07
    
    second_customer:
      id:               2
      customer_code:    456
      created_at:       2007-09-26 11:17:13
      updated_at:       2007-09-26 11:17:13
  9. And here are some functions to test some pages and a full CRUD test
  10. require File.dirname(__FILE__) + '/../test_helper'
    require 'customers_controller'
    
    class CustomerControllerTest < Test::Unit::TestCase
      fixtures  :customers
    
      def setup
        @controller = CustomersController.new
        @request = ActionController::TestRequest.new
        @response = ActionController::TestResponse.new
      end
    
      def test_index
        get :index
        assert_response :success
        assert_template 'list'
      end
    
      def test_new
        get :new
        assert_response :success
        assert_template 'create_form'
      end
    
      def test_edit
        get :edit, :id => customers(:first_customer).id
        assert_response :success
        assert_template 'update_form'
      end
    
      def test_show
        get :show, :id => customers(:first_customer).id
        assert_response :success
        assert_template 'show'
      end
    
      def test_customer_controller_CRUD
    
        # CREATE
        # Get the number of records
        record_no = Customer.count
        # Create a new record
        post  :create, {"commit"=>"Create", :record=>{"customer_code"=>"890"}}
        # Assert that the record is not nil
        assert_not_nil  assigns("record")
        # Look that the number of records has been increased by 1
        assert_equal  record_no+1, Customer.count
    
        # UPDATE
        # Get the number of records
        record_no = Customer.count
        # Update a record
        new_customer_code = "987"
        put :update, {"commit"=>"Update",:id=>customers(:first_customer).id,
                                          :record=>{"customer_code"=>new_customer_code}}
        # Assert that the record is not nil
        assert_not_nil  assigns("record")
        # Look that the number of records has stayed the same
        assert_equal  record_no, Customer.count
        # Check that the update took place
        customer = Customer.find(customers(:first_customer).id)
        assert_equal  new_customer_code, customer.customer_code
    
        # DELETE
        # Get the number of records
        record_no = Customer.count
        # Delete a record
        delete  :destroy, :id => customers(:first_customer).id
        # Look that the number of records has been decreased by 1
        assert_equal  record_no-1, Customer.count
      end
    
    end

dhtmlcalendar in ActiveScaffold

To control the date format in dhtmlcalendar within ActiveScaffold in Ruby on Rails, to make the date of 2 November 2007 appear like 02-11-2007, use:

i) if you want to affect the settings in all the application add the following to the top of your controllers/application.rb

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMAT[:default] = ‘%d-%m-%Y’

ii) If you only want to affect the settings of the calendar in one specific controller, then add the same line only to the controller that you want:

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMAT[:default] = '%d-%m-%Y'