Ruby on Rails
(was used in class at SIIT)
Keeps "refresh" this page for new info ;)
Ruby (programming language)
Ruby on Rails (web application framework) - Documentations, API references
How to install Ruby + Rails on Windows
Rails framework is heavily based on Model-View-Controller software pattern
Model for data, View for presentation, Controller for logic
Other resources: Pok's blogger , Codenone
Today, we're going to write our own weblog (blog) application. Don't worry if you can't finished it in the class, but try to finished the whole lot before next class. Cheers! ;)
Create new Rails webapp by using "rails" command, the syntax is "rails <project_name>"
rails myblog
Go into the project directory
cd myblog
Run WEBrick web server, to test our project
ruby script/server &
The "ruby" in the command above is to call Ruby interpreter to execute the "server" script in "script" directory.
"&" at the end tells the UNIX shell to fork out the process, so you can still typing commands at the prompt while the web server is running.
Point your web browser to http://localhost:3000/ , see what happens ?
You can press Ctrl-C at any time to terminate the web server.
Execute this command
ruby script/generate controller Blog
This will creates a controller named "blog".
Point your web browser to http://localhost:3000/blog , see what happens ?
Modify app/controllers/blog_controller.rb, add "index" function to the BlogController
class BlogController < ApplicationController
def index
render :text => "Hello, World!"
end
end
Refresh the page http://localhost:3000/blog again, what happens ?
Makes index function blank, or commented it out.
class BlogController < ApplicationController
def index
# render :text => "Hello, World!"
end
end
Refresh the page http://localhost:3000/blog again, what happens ?
The try to delete the whole "index" function out, can you guess the result ...?
Create new file "index.rhtml"in app/views/blog directory
Put some HTML text in that rhtml file, for example:
<p>Hello from Template!</p>
<p>Current time is <%= Time.now %></p>
Save the file, refresh the page http://localhost:3000/blog again, what you see ?
Prepare a database for our webapp, using MySQL command-line program.
mysqladmin -u root create myblog_development
In this case, we created a database named after the project name, <project_name>_development. You can use other name for the database, freedom! -- but in that case, you need to modify config/database.yml to suited your actual configuration. Also, if you use something that is not MySQL on localhost, you need to modify the config file as well.
Create a model named "Post"
ruby script/generate model Post
Modify the migration script, db/migrate/001_create_posts.rb Add these codes to "self.up" function
def self.up
create_table :posts do |t|
t.column :title, :string
t.column :body, :text
t.column :author, :string
t.column :created_at, :datetime
end
end
Then run the whole migration scripts, using Rake. rake db:migrate
Modify BlogController (in app/controllers/blog_controller.rb),
add scaffold statement to the class
class BlogController < ApplicationController
scaffold :post
end
Run the web server, see the page http://localhost:3000/blog .
Have we got CRUD already ?
What is CRUD ? - Create, Read, Update and Delete
Rails generates basic database functionalities for us, using only one "scaffold :<model_name>" statement! :)
In Rails, validations of any model should be described in the model itself.
So, to add validations to the Post model, we modify app/models/post.rb
class Post < ActiveRecord::Base
validates_presence_of :title
end
Test the validation, create new record in your blog but omitted the title. See what happens ?
Try to add other validations, say Author name should be at least 5 characters long.
One of the most exciting part of Rails framework is this one, scaffolding. It creates lots of "boiler plate code" for work, codes that are so redundant, but you need to have them anyway. With scaffold generator, you don't have to do everything by yourself. Just ask Rails to generate templates for you, and modify those templates to fit your needs, selectively.
How to use scaffold generator ?
ruby script/generate scaffold
The command above will prints you out how to use scaffold generator.
Let's do one for your blog. A Blog controller that interacts with Post model.
ruby script/generate scaffold Post Blog
You will be asked if you like the script to overwrites some of the files. N for No / Y for Yes / A for Yes to all. In this tutorial, we say "A" - overwrites everything.
Modify public/stylesheets/scaffold.css and app/views/blog/list.rhtml also other *.rhtml as you wish.
Mainly .rhtml is for content/data, and .css is for style/presentation.
Currently, the blog show every entries in a table ... not very fancy, too boring.
Try to understand the code in list.rhtml, and modify it.
What the different between <%= ... %> and <% ... %> ?
Look particularly at the objects like @posts and post. What are they ? How to access information in these objects in order to display them to your blog ?
To finished this part, let yourself do some guess work, trail-and-errors.
For example, what if you print post.body out?
Consults Documentations and API references if you really have no idea.
Blog has comments, so you have to create a new model for comments as well.
Try to design your own first. Think about it. How each comment be attached/related to each blog post ?
After you have designed your own, look at the tutorial vdo (below), compare them ... which one is better in your opinion ? ... implement the better one :)
... see the rest of the tutorial in Ruby on Rails Screencasts, choose "Create a weblog in 15 minutes". Mind you that you have to adapt something to fit your own environment (the tutor use Mac OS X).
Do the rest of the tutorial you found in the VDO. Questions can be posted on the siit.net webboard, or direct to me at arthit at gmail dot com -- Enjoy !! :)