Now that you've coded the interface, the next step is to set up your Rails application. First, you need to create a new database. This tutorial uses MySQL, but Rails is capable of supporting many other databases, too. Open your MySQL Command Line Client and enter the following line:
CREATE flex_issue_tracker_development
That is all the SQL you are going to need to write. Rails will take care of the rest.
To create your Rails application, open up a command-line window and change the directory to the location that you would like to use for your application. I generally use C:\Dev. Once you're in your desired location on your server, enter the following line:
rails issue_tracker
This command generates all of the folders and files that you will need for your Rails application. Part of the power of Rails is that it promotes consistency across projects by ensuring that all Rails applications have the same structure.
Next, create the model for this application. In Rails, the model is the code that wraps a table in your database. To create your model, enter the following line:
cd issue_tracker
To change the directory to your Rails app.
Next, enter the following line:
ruby script/generate model bug
Finally, you'll create the controller. The controller contains the logic for interacting between the view (in this case Flex) and the model. To create your controller enter the following line:
ruby script/generate controller bugs
Now, inside your Rails application folder, go to app > controllers. You will see the bugs_controller.rb file that you just generated. Open this file and add the following code:
def create @bug = Bug.new(params[:bug]) @bug.save render :xml => @bug.to_xml end def list @bugs = Bug.find :all render :xml => @bugs.to_xml end def update @bug = Bug.find(params[:id]) @bug.update_attributes(params[:bug]) render :xml => @bug.to_xml end def delete @bug = Bug.find(params[:id]) @bug.destroy render :xml => @bug.to_xml end
These methods implement basic CRUD functionality, where the list method corresponds to read. The following is a brief explanation of the key parts of this code:
params: This variable is a hash that is automatically populated with the values that you pass an argument to the Rails method.Bug: This refers to the ActiveRecord that you created when you generated your model. Note that it is a convention in Rails for the table name to be the plural of the model's name. Using this convention, when Rails encounters a model called "Bug" it looks for a table called "bugs" in the database.find: The find method searches the records in a database for the specified table. Bug.find :all returns all of the records for the bugs table. Bug.find(params[:id]) returns the first record in the bugs table whose id matches the value contained in the params hash. Please note that it is also a convention in Rails for each table to have column named id that is the primary key.new: This creates a new ActiveRecord instance.save: This saves the record to the database.update_attributes: This updates the record using the values provided in the hash.destroy: This deletes a record from the database.to_xml: This builds XML document from the model.return, Ruby methods return the value of the last line inside the method.Now you must specify to your Rails application how to access the database that you just created. To do this, go to your Rails application folder (e.g., C:\Dev\issue_tracker) and open up the config folder. Locate the database.yml file and open it. Edit this file so that it reads as follows:
development: adapter: mysql database: flex_issue_tracker_development username: <your username here> password: <your password here> host: localhost
Be sure to fill in your own username and password. For this tutorial, you are only going to be running Rails in development mode, so you do not need to worry about the production and test sections of the file.
Now that your Rails app can access your database, your next step is to add a table to your database for storing your bugs. The easiest way to do this is to use Rails migrations. Migrations virtually eliminate the need for using SQL statements to set up your database. Using migrations also has many other benefits, such as being database agnostic; a discussion of the benefits is beyond the scope of this tutorial.
To implement the migration for your app, go to db > migrate. You will see a file called 001_create_bugs.rb that was automatically created when you created your model. Edit this file to look like the following:
class CreateBugs < ActiveRecord::Migration
def self.up
create_table :bugs do |t|
t.column :reportedby, :string
t.column :assignedto, :string
t.column :description, :text
t.column :status, :string
t.column :priority, :string
end
end
def self.down
drop_table :bugs
end
end
The up method creates a table called bugs with six columns:
Note that the column names correspond to the values in the dataField attributes in the DataGrid column that you created in your Flex interface. The down method removes the table.
To run the migration, go to the command line and enter:
rake migrate
Now, if you go back to your MySQL Command Line Client and type:
USE flex_issue_tracker_development
DESCRIBE bugs;
You will see the following in the command-line window:

Figure 1. The command-line window
Finally, set up your Rails app to boot up the server. Ruby comes with its own handy server called WEBrick. To start WEBrick go to the command line and enter:
ruby script/server
Note that the server defaults to running on port 3000. This will be important in the next section.
That's it. Your Rails app is all ready. If you open up an Internet browser and type http://localhost:3000 you will see the Rails welcome page.