The Depot Application
I’ll take the creating a depot application of books for maintaining and buying as an example. Of course, this application has database operation( I use MySQL here ).
Create database:
depot>mysql –u root create depot_development
After create database, you should modify the configuration in the config/database.yml, set the database name, user name and password.
If you want to make sure the job you just did is succeed or not, you could use
depot >rake db:migrate
if not return the error message, that means the job has succeeded.
Create the Product Model and Table:
I use the migration to create and maintain the database structure.
depot >ruby script/generate model product
this generator create a bunch of files. The two we are interested are the model itself, product.rb, and the 001_create_products.rb.
because I want to use migration to maintain the database, let me write code of creating table in migration files.
work/depot/db/migrate/001_create_products.rb
class CreateProducts < ActiveReacord::Migration
def self.up
create_table :products do |t|
t.column :title, :string
t.column :description, :text
t.column :image_url, :string
end
end
def self.down
drop_table :products
end
end
we tell rake to apply the unapplied migration to the database.
depot>rake db::migrate
if you want to rolling back the migration, just use
depot>rake db::migrate VERSION=0
Create Controller
Because we’re using the MVC architecture, our application need a controller to coordinate the stuff of maintaining the list of products.
depot>ruby script/generate controller admin
after create the controller, all the background work has been finished, we should connect the application with the development database.
depot/app/controllers/admin_controller.rb
class AdminController < ApplicationController
scaffold :proudct
end
the scaffold tell rails to dynamic generate the application code, if you want to change the view style, using this method would have some problems. In this case, you should use static scaffold. I’ll introduce it later.
Now, you can start up the web server to check the job we just finished.
Add a Missing Column
After we create the table, we may find that we missing a column, then we can add the column in the database directly
mysql>alter table products add column price decimal( 8, 2 )
but we use migration before, using migration to add the new column will give us a version-controlled history of the schema and it is simple to recreate it.
depot>ruby script/generate migration add_price
this cmd creates the 002_add_price.rb in the migrate directory. Open the file and edit it.
depot/db/migrate/002_add_price.rb
class AddPrice < ActiveRecord::Migration
def self.up
add_column :price, :decimal, :precision => 8, :scale => 2, :deafult => 0
end
def self.down
remove_column :products :price
end
end
In order to apply this migration to the database, please remember
depot>rake db::migrate
Refresh your browser, you’ll see the price when you new the product.
Validate
In order to avoid the invalid value put into the table, we should put the validation in the model.
Depot/app/models/product.rb
class Product < ActiveRecord::Base
validates_presence_of :title, :description, :image_rul
validates_numericallty_of :price
validates_uniqueness_of :title
validates_format_of :image_url,
:width => %r{/.(gif|jpg|png)$}i
:message => "must be a URL for a GIF, JPG, or PNG image"
protected
def validate
errors.add(:price, "should be at least 0.01") if price.nil? || price < 0.01
end
end
Rails will automatically call the validate method.
Prettier Listings
If we want to change the view code, we must use the static scaffold.
depot>ruby script/generate scaffold product admin
this cmd checks to make sure we have the model file and create the views files need to display the maintain screens. When it reaches the controller, it detected the admin_controller.rb file has been modified, asking the permission to overwrite the file. The only change we made to this file was adding the scaffold :product line, which we on longer need, so we agree.
After this cmd finished, there are several files has been created in the views directory, we could prettier the views by editing the files.
Stylesheet
On another side, we need to write CSS file to style the presentation, and put this file at some place, tell the browser to fetch it.
All scaffold-generated application use the scaffold.css in the directory public/stylesheets. Rather than alter the file, we create a new file named depot.css and put it in the same directory.
Finally, we need to link the stylesheets into our HTML pages, we are no need to write the reference in the HEAD tag in the rhtml files. Rails keep a separate file to create the environment for the admin pages. The file, called admin.rhtml, is a layout page and live in the views/layout.
The only thing we need do is put the ‘depot’ in the
<%= stylesheet_link_tag ‘scaffold’ %>
to make it change to
<%= stylesheet_link_tag ‘scaffold’, ‘depot’ %>
After this, all the ground work has been finished, you can access the pages to see the efficiency.