Improved Scaffolding for Ruby on Rails
Let's begin by fixing the order of our columns. Change the app/controllers/player_controller.rb file to look like this:
class PlayerController < ApplicationController active_scaffold :player do |c| c.columns = [:name, :squad, :address, :date_of_birth, :contact_tel_no, :condition ] c.columns[:squad].ui_type = :select c.columns[:condition].ui_type = :select end end
In this code, we provide a configuration code block to the activescaffold method where we specify the ordering of the columns, in addition to setting the ui_type associated with the squad and condition data to be :select. This fixes our ordering issue and sets the squad and condition selection mechanism to a standard drop-down list.
Sorting out the date problem requires the creation of a Rails helper method for the players table. Edit the app/helpers/player_helper.rb file, and add the following code:
module PlayerHelper def date_of_birth_form_column(record, input_name) date_select :record, :date_of_birth, :name => input_name, :start_year => 1990 end end
The oddly named date_of_birth_form_column helper method calls the ActiveScaffold-supplied date_select method, which lets us adjust the earliest start date associated with our date_of_birth data. With these changes made, restart the Rails application and reload the browser window. Figure 3 shows the new-and-improved player listing, and Figure 4 shows the final version of our player data-entry form. As I'm sure you'll agree, both screens look the business. Take time to play around with the added functionality that ActiveScaffold has provided for free, including sort-ordering links on each of the column headings.
To learn more about Rails, I highly recommend Agile Web Development with Rails by The Pragmatic Programmers (now in its second edition, with a third due soon), as well as O'Reilly Media's Rails Cookbook. To learn more about ActiveScaffold, check out the well-written documentation and code examples available on-line at the ActiveScaffold Web site (see Resources). As I hope this article demonstrates, it doesn't take much to turn an ugly, default Rails application into something you just might want to show off!
Ruby on the Web: www.ruby-lang.org
The RubyGems RubyForge repository: rubyforge.org/projects/rubygems
ActiveScaffold Web Site: activescaffold.com
Rails Plugin Repository: agilewebdevelopment.com/plugins
“An Ajax-Enhanced Web-Based Ethernet Analyzer” by Paul Barry (LJ, May 2007): www.linuxjournal.com/article/9614
“A Database-Driven Web Application in 18 Lines of Code” by Paul Barry (LJ, March 2005): www.linuxjournal.com/article/7937
Reuven Lerner's Excellent Series of Articles Comparing PostgreSQL to MySQL (April, May and June 2007 issues of LJ): www.linuxjournal.com/article/9571, www.linuxjournal.com/article/9618 and www.linuxjournal.com/article/9649
Web Development News
|The True Internet of Things||Sep 02, 2015|
|September 2015 Issue of Linux Journal: HOW-TOs||Sep 01, 2015|
|September 2015 Video Preview||Sep 01, 2015|
|Using tshark to Watch and Inspect Network Traffic||Aug 31, 2015|
|Where's That Pesky Hidden Word?||Aug 28, 2015|
|A Project to Guarantee Better Security for Open-Source Projects||Aug 27, 2015|
- The True Internet of Things
- Using tshark to Watch and Inspect Network Traffic
- September 2015 Issue of Linux Journal: HOW-TOs
- Problems with Ubuntu's Software Center and How Canonical Plans to Fix Them
- Concerning Containers' Connections: on Docker Networking
- Firefox Security Exploit Targets Linux Users and Web Developers
- Where's That Pesky Hidden Word?
- A Project to Guarantee Better Security for Open-Source Projects
- Build a “Virtual SuperComputer” with Process Virtualization
- My Network Go-Bag