Search code examples
ruby-on-rails-3formsactivescaffold

How to create nice forms with active_scaffold gem on Rails 3


  1. How to override Active Scaffold form fields for date or time? (datepicker and calendar_date_select didn't work for me, probably because I'm using the activescaffold gem)
  2. How to override Active Scaffold form to select from a list of resources in the database?

Thanks.


Solution

  • I was struggling this question until I figured it out. Here's an example:

    class Player < ActiveRecord::Base
      belongs_to :game
      attr_accessible :name
    end
    
    class Game < ActiveRecord::Base
      has_many :players 
      attr_accessible :thedate, :thetime, :winnername
    end
    
    class GamesController < ApplicationController
      active_scaffold :game do |conf|
        # do nothing in this example
      end
    end
    
    module GamesHelper
    
      # date select
      def game_thedate_form_column (record, options)
        date_select :record, :thedate, options
      end
    
      # time select
      def game_thetime_form_column (record, options)
        time_select :record, :thetime, options
      end
    
      # select from database resources
      def game_winnername_form_column (record, options)
        select_tag :winnername, options_for_select(get_players_names_arr(record)), options
      end
    
      def get_players_names_arr(game)
        names = []
        game.players.each do |player|
          names << player.name
        end
        names 
      end
    
    end