Creating a Ruby Weblog in 10 Minutes
来源:互联网 发布:网络图书馆系统 编辑:程序博客网 时间:2024/05/21 06:35
Contributed by Brian Leonard, maintained by Gail Chappell
December 2007 [Revision number: V6.0-6]
In this tutorial, you use the Ruby support in the NetBeans IDE to create and run a simple web application. The example shows how to create a Ruby web log. You follow the basic workflow of creating the model, adding a controller, and creating a view.
Contents
- Tutorial Requirements - Creating the Sample Database - Creating the Ruby on Rails Project - Configuring the Database Environment - Creating the Model - Migrating the Database - Creating a Controller - Running the Application - Doing More: Adding Another Field - Doing More: Validating Input - Doing More: Making the List Look More Like a BlogTutorial Requirements
This tutorial requires the following technologies and resources:
- A database server
- NetBeans IDE 6.0 with Ruby support
Note: This tutorial, which is written for Rails 1.2.5, involves scaffolding. As of Rails 2.0, scaffolding is no longer supported.
Creating the Sample Database
Note: This tutorial uses the MySQL database server. See the Installing and Configuring Ruby Support article for information about using a MySQL database server in a Ruby application. The article also describes how to use the Java DB database server instead.
Before you create the Ruby on Rails project, create a rubyweblog_development database, as described below.
- Open a command window.
- If it has not already been started, start the MySQL database server.
- Type the following command to create the development database and press Enter.
mysqladmin -u root -p create rubyweblog_development
Note: If the root user does not have a required password, omit the -p argument.
Creating the Ruby on Rails Project
- In the NetBeans IDE, choose File > New Project.
-
Select Ruby in the Categories field and Ruby on Rails Application in the Projects field. Click Next.
Note: The first time that you create a Ruby project in the IDE, the IDE checks if you have any other Ruby installations in addition to the bundled JRuby software. If you do, the IDE displays a dialog box asking you to select which software to use. Choose JRuby if you want to use the bundled JRuby interpreter, or choose your Ruby installation if you prefer to use it instead. For more information, see Configuring the IDE to Use Your Own Ruby Installation in the Installing and Configuring Ruby tutorial. - Type
rubyweblog
in the Project Name field. Accept all the other default settings. -
Click Finish to create the new project.
The IDE creates the project directory with the same name as your project. You see the following:
- The basic categories of the application in the Projects window. Of particular interest are the Controllers, Models, and Views nodes. In this tutorial, you follow the basic workflow of creating the model, adding a controller, and creating a view.
- A list of files that are part of the application in the Output window. You can click a link in the Output window to open a file in the editing area.
- The
database.yml
file in the editing area.
Configuring the Database Environment
- In the editing area, edit the
database.yml
by providing the password in the development configuration. -
Save and close the
Note: If your operating system's host file does not contain localhost, use 127.0.0.1 instead.database.yml
file.
Creating the Model
Here you use the Rails Generator to create a model for the application. The rubyweblog application requires a Post model for storing instances of blog posts.
- In the Projects window, right-click the Models node and choose Generate.
-
In the Rails Generator dialog box, type
Post title:string
in the Arguments field and click OK.The Rails Generator creates a model named Post. The Output window lists the files that are created as part of the model generation:
- app/models/post.rb. A file that holds the methods for the Post model. This file is also opened in the editing area.
- test/unit/post_test.rb. A unit test for checking the Post model.
- test/fixtures/posts.yml. A test fixture for populating the model.
- db/migrate/migrate/001_create_posts.rb. A migration file for defining the initial structure of the database.
Migrating the Database
001_create_posts.rb
. -
In the Output window, click the link for the
001_create_posts.rb
file.The file opens to show the
Code Sample 1: Code forself.up
method, which creates a table called posts, and theself.down
method, which tears the posts table down, as shown in the following code sample:001_create_posts.rb
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.column :title, :string
end
end
def self.down
drop_table :posts
end
end
-
In the Projects window, right-click the rubyweblog node and choose Migrate Database > To Current Version.
This action updates the the database to include the posts table. The Output window indicates when the migration is complete.
Creating a Controller
-
In the Projects window, right-click the Controllers node and choose Generate.
-
In the Rails Generator dialog box, type
This action creates the fileBlog
in the Name field. Leave the Views field blank. Click OK.blog_controller.rb
and opens the file in the editing area.
A blog_controller.rb node is added under the Controllers node in the Projects window. -
Edit
Code Sample 2: Code forblog_controller.rb
by adding the following scaffolding code, which provides a simple CRUD application around the Post model:blog_controller.rb
class BlogController < ApplicationController
scaffold :post
end
Running the Application
-
Under the Configuration node, open
routes.rb
. Find the line:# map.connect '', :controller => "welcome"
- Edit the line by removing the comment sign (#) and changing
welcome
toblog
. -
Expand the Public node, right-click index.html and choose Delete.
index.html
displays a default Welcome page, which is not what you want. By deletingindex.html
, Rails looks inroutes.rb
to figure out what page to display, which you set to the blog in the previous step. - Choose File > Save All.
-
Click the Run Main Project button in the toolbar.
This action starts the WEBrick server, which is part of the Ruby on Rails framework, and launches the web browser. Following is the first page of the application.
Figure 1: rubyweblog Home Page
-
Click the New post link to display the second page of the application, shown below.
Figure 2: Page for Creating a New Post
-
Enter a title and click Create.
Following is a sample blog post.
Figure 3: Successful Creation of Blog Post
Doing More: Adding Another Field
-
Right-click the Database Migrations node and choose Generate. In the Rails Generator dialog box, type
AddBody
in the Arguments field and click OK.The IDE creates the versioned migration script
002_add_body.rb
and opens the file in the editing area. -
Open up a line under def self.up, type mcol, and press Tab.
The IDE replaces the mcol trigger with the following code template with 3 parameters:
add_column :table, :column, :string
-
Type posts to replace the first parameter, then press Tab. Type body and press Tab again. Then type text to replace the third parameter. The line should look like the following statement:
add_column :posts, :body, :text
This migration adds a body column to the posts table. - Choose File > Save All.
-
Right-click the rubyweblog node and choose Migrate Database > To Current Version.
Alternatively, right-click in the source file and choose Run from the pop-up menu. -
Return to the browser and click the New Post link to see how Ruby recognizes the new body field, shown in the following figure.
Figure 4: New Post With Body Field
-
Create a few more blog entries. For example:
Figure 5: More Blog Posts
Doing More: Validating Input
- In the Projects window, expand the Models node and double-click post.rb to open the file in the editor.
-
Open up a line inside the Class definition, type vp, then press Tab.
The IDE replaces vp trigger with the following code template:validates_presence_of :attribute
- Type title, :body. The code should look like the following statement:
validates_presence_of :title, :body
-
Run the application, click New Post, and click Create.
The application now reports that the title and body cannot be blank.
Doing More: Making the List Look More Like a Blog
- In the Projects window, right-click the Views node and choose Generate.
- In the Rails Generator dialog box, choose scaffold from the Generate drop-down list.
-
Type
The IDE creates a view for the Post model and lists the contents in the Output window.Post
in the Model Name field andBlog
in the Controller Name field. Leave the Actions field blank. Select Overwrite to force the BlogController to be regenerated, and then click OK. -
Expand Views > blog and open
Code Sample 4: Code forlist.rhtml,
which is used to show the list of blog entries. Delete the <h1> and <table> tags and replace them with the following code:list.rhtml
<h1>The Ruby Blog</h1>
<% @posts.each do |post| %>
<h2><%= post.title %></h2>
<p><%= post.body %></p>
<small> <%= link_to 'Permalink', :action => 'show', :id => post %></small>
<hr>
<% end %>
For each instance of a
Tip: liai (link with an action and index) is the trigger for the following code template:post
action, this code produces a title, body, and Permalink, as shown in Figure 6.<%= link_to "link text...", :action => "edit", :id => @item %>
-
Run the application to see the new interface for the Post model.
Figure 6: New and Improved Model Interface
-
To display the blog with the most recent entry first, reverse the sort order by adding .reverse to the end of @posts in
list.rhtml
:<% @posts.reverse.each do |post| %>
When you save the file and refresh your browser, the blog displays as shown in the following figure.
Figure 7: Blog Posts in Reverse Order
Next Steps
-
To continue with the Ruby web log tutorial and learn more about Ruby support in the NetBeans IDE, go to Building Relationships Between Rails Models.
-
To obtain support and stay informed of the latest changes to the NetBeans Ruby development features, join the users@ruby.netbeans.org and dev@ruby.netbeans.org mailing lists.
- To submit your own NetBeans Ruby tutorial, visit the NetBeans Community Docs page.
- Creating a Ruby Weblog in 10 Minutes
- Ruby in twenty minutes(4/4)
- 在 10 分钟之内创建 Ruby Weblog
- 在 10 分钟之内创建 Ruby Weblog
- XML in 60 Minutes a Day
- Setup a Subversion Server in 4 Minutes
- Setup a Subversion server in three minutes
- Learn UNIX in 10 minutes
- learn xajax in 10 minutes
- Learn UNIX in 10 minutes
- Regular Expressions in 10 Minutes
- Learn Python in 10 Minutes
- front UAG in 10 minutes
- Learn Python in 10 minutes
- learn python in 10 minutes
- database:SQL in 10 minutes
- Creating a Plug-In Framework
- Creating a Plug-In Framework
- 应用1-用Silverlight做五星红旗
- 应用2-制作简易版奥运五环
- 入门5-Silverlight中如何进行图片处理
- 和龙---永远的传说.
- vb 获取系统信息
- Creating a Ruby Weblog in 10 Minutes
- SAP BW数据仓库简介
- CBrowser的世界很精彩
- 入门6-Silverlight 1.0中多媒体影音的实现
- memcached 介绍
- the Asia-Pacific Region Pirate Bay
- 正则收集
- .Net下实现分布式缓存同步的手
- 各个大学资源链接:北大,北邮,清华,中科院,上海交大 ..........