蓝天,小湖,湖水中一方小筑

RoR learning log (1)

Just back from vacation, so the study log for this week is simple.

Build development environment

The whole process is tested on Mac OS, and I think this may have little difference with Linux.

Ruby should be installed before trying Rails. To separate with the Ruby used by system, it is recommended to use RVM (Ruby version manager) to build the local Ruby environment (seems like the virtualenv in Python). Here is the install script from RVM installing page

$ curl -L get.rvm.io | bash -s stable
$ source ~/.rvm/scripts/rvm
$ rvm requirements
$ rvm install 1.9.3

After installing, the command rvm use can be used to select the version of Ruby.

Next per-requisite is gem, which can be get from this page

Then we can use gem to install rails

gem install rails

A simple TODO list

This section will implement a todo list application, which is copied from bottle tutorial page. Here is the goal copied from that page:

At the end of this tutorial, we will have a simple, web-based ToDo list. The list contains a text (with max 100 characters) and a status (0 for closed, 1 for open) for each item. Through the web-based user interface, open items can be view and edited and new items can be added.

This log only create the default controller, and some other thing such as adding validator and modifying template will be shown in next log.

First, we have to create the directory for application. We named the application to todo_app, and we run this command to do the initialization:

$ rails new todo_app

After plenty of output message, we have a new directory named todo_app, and the directory structure has been initialized in it. After running rails server in the directory, we can access the default website via http://127.0.0.1:3000

Next step is generate database schema for this application. As mentioned above, the schema contains two fields, one is record content, the other is status of record. Rails provide a generator script to simplify the operation.

$ rails generate scaffold ToDo content:string status:integer

Then use rake command to migrate the database

$ rake db:migrate

This script will generate URL mapping, model and default view for the ToDo table. You can use http://127.0.0.1:3000/to_dos to access the new added things (You can check the config/routes.rb to find the correct mapping). There is no data currently, but there is a link to add new record, which is linked to http://127.0.0.1:3000/to_dos/new. And after record added, there will be show, edit and destroy action to each record.

The controller, view and model is saved in app directory. The file app/controllers/to_dos_controller.rb will handle the request sent from browser, the file app/models/to_do.rb is the model of ToDo record, and the files under app/views/to_dos/ will show the returned record. In this demo project, we need add some validation while adding ToDo record, and modify the list page for updating the ToDo record.

For now, there is a simple web application, can use generated view/controller/model to manager ToDo list. In the next log, this application will have a validator and new template.