- 浏览: 70341 次
- 性别:
- 来自: 北京
最近访客 更多访客>>
最新评论
-
kldwq2002:
随便搜到这来的。看出您的代码有啥问题么?Exception被c ...
如何在Android当中显示网络图片 -
lamborghini_lp:
最近在看Ruby元编程,感觉不容易啊。
ruby元编程(入门收藏) -
boobmoom:
好东西 以后还得来看
ruby元编程(入门收藏) -
carlosbdw:
赞一个!欢迎来j-kanban.com看看!
转一篇好文章:用Rails实现“乐道”构想 -
akane:
纯净口水?
和NibiruTech团队交流后总结(最主要是和Kevin的交流)
使用NeatBeans十分钟开发一个ruby on rails 的项目
这个教程是neatbeans上边的。
学习的时候注意一下数据库的配置:
a.将mysql里边 my.ini里边的默认语言设置改成 utf8。
b.在项目的数据库配置文件里边,也就是database.yml文件里边增加一个字符编码的设定。如下:
- development:
- adapter: mysql
- database: test
- username: root
- password: root
- host: localhost
- encoding: utf8
Creating a Ruby Weblog in 10 Minutes
Contributed by Brian Leonard, maintained by Gail Chappell
September 2007 [Revision number: V6.0-4]
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.
Tutorial Requirements
This tutorial requires the following technologies and resources:
- A database server
- NetBeans IDE 6.0 with Ruby support
Creating the Sample Database
Note: This tutorial uses the MySQL database server. See Installing and Configuring Ruby Support for information about using a MySQL database server in a Ruby application. This document also contains tips on 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 to see 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 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.
Configuring the Database Environment
- Open Configuration > database.yml
- Edit the
database.yml
by providing the password in the development configuration. -
Save and close the
Important Note: If your operating system's host file does not contain localhost, use 127.0.0.1 instead. Note also that with some systems, the database setting must be in lower case letters.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
in the Arguments field and click OK.The Rails Generator creates a model named Post. The Output window lists that 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
. You add information to configure the database. -
In the Output window, click the link for the
001_create_posts.rb
file.The file opens to show the
self.up
method, which creates a table called posts, and theself.down
method, which tears the posts table down. -
Add the title column (shown in bold) to
create_table
in theself.up
method as shown in the following figure:Code Sample 1: Code for 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
- From the main menu, choose File > Save All.
-
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
blog_controller.rb
by adding the following scaffolding code, which provides a simple CRUD application around the Post model.Code Sample 2: Code for blog_controller.rb
class BlogController < ApplicationController scaffold :post end
Running the Application
-
Under the Configuration node, open
routes.rb
. Find the following 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 New post.
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 and the versioned migration script
002_add_body.rb
and opens the file in the editing area. -
Modify
002_add_body.rb
as follows:Code Sample 3: Code for 002_add_body.rb
class AddBody < ActiveRecord::Migration def self.up add_column 'posts', 'body', :text end def self.down remove_column 'posts', :body end end
This migration adds a body column to the posts table and removes it again, if you are backing out of the migration. - Choose File > Save All.
- Right-click the RubyWebLog node and choose Migrate Database > To Current Version.
-
Return to the browser and click the New Post link to see how Ruby recognizes the new body field.
Figure 4: New Post With Body Field
-
Create a few more blog entries. For example:
Figure 5: More Blog Posts
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
list.rhtml,
which is used to show the list of blog entries. Delete the <h1> and <table> tags and replace them with the following code:Code Sample 4: Code for 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
post
action, this code produces a a title, body and Permalink, as shown in Figure 6. -
Choose File > Save All and then refresh your browser 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
相关推荐
在过去的几年中,《Ruby on Rails Tutorial》这本书被视为介绍使用 Rails 进行 Web 开发的先驱者。 在这个全球互联的世界中,计算机编程和 Web 应用程序开发都在迅猛发展,我很期待能为中国的开发者提供 Ruby on ...
《Ruby on Rails 101》是一本介绍Ruby on Rails(简称RoR或ROR)的基础书籍,旨在为初学者提供一个全面而深入的学习框架。本书由Peter Marklund编写,包含了五天课程的演示文稿和相关资料,覆盖了从安装到实际应用...
Ruby on Rails,简称Rails,是基于Ruby编程语言的一个开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构模式,旨在提高开发效率和代码的可读性。Rails以其“约定优于配置”(Convention over Configuration)...
在这个开发环境包中,我们拥有Ruby 1.8.7版本和Rails 2.2.3版本,这两个组件构成了一个经典的开发组合。 Ruby 1.8.7是Ruby编程语言的一个早期稳定版本,发布于2010年,提供了许多改进和新特性。在Ruby 1.8.x系列中...
在这个阶段,你将学习如何使用命令行工具来创建一个新的Rails项目,并理解Rails的目录结构及其各自的作用。 接下来,你将深入到MVC架构中,学习如何创建数据模型(Models),这是Rails应用程序的核心部分,用于与...
**Install Ruby Rails Lighttpd MySQL on Ubuntu or FreeBSD - Huihoo Wiki - Open Source Wiki.htm**: 这是一个安装指南文档,详细描述了如何在Ubuntu或FreeBSD系统上安装Ruby、Rails、Lighttpd和MySQL,形成一个...
Ruby on Rails,简称Rails,是一款基于Ruby语言的开源Web应用框架,它遵循MVC(Model-View-Controller)架构模式,旨在简化Web应用程序的开发。Rails由David Heinemeier Hansson于2004年创建,它提倡“约定优于配置...
本书教您如何使用Ruby on Rails开发和部署真正的,具有工业实力的Web应用程序,Ruby on Rails是为诸如Twitter,Hulu,GitHub和Yellow Pages等顶级网站提供支持的开源Web框架。
- **步骤**:通过命令行使用`rails new project_name`来初始化一个新的Rails项目。 - **结构**:新项目将包含默认的目录结构,如`app`、`config`、`db`等,分别用于存放应用程序代码、配置文件、数据库迁移脚本等。 ...
Web开发:Ruby on Rails.pdf
在“ruby on rails社区网站开发源码”中,我们可以学习到如何利用Rails构建一个互动性强、功能丰富的社区网站。以下是一些关键知识点: 1. **安装与环境设置**:首先,你需要安装Ruby和Rails。这通常涉及设置Ruby...
Ruby on Rails(简称Rails)是基于Ruby编程语言构建的一个开源Web应用程序框架,它遵循“约定优于配置”(Convention over Configuration, CoC)的原则,以及“Don't Repeat Yourself”(DRY)的设计理念,极大地...
本示例源码提供了使用Ruby on Rails进行实际项目开发的具体实践,帮助开发者深入理解Rails的工作原理和最佳实践。 在Rails中,`模型`负责处理数据和业务逻辑,`视图`负责展示用户界面,而`控制器`则作为模型和视图...
Ruby on Rails,简称Rails,是基于Ruby语言的一个开源Web应用程序框架,它遵循MVC(Model-View-Controller)架构模式,旨在使Web开发过程更加高效、简洁。本压缩包中的"Ruby on Rails入门经典代码"提供了新手学习...
- 对于正在寻找高效Web开发框架的企业和个人开发者,Ruby on Rails是一个值得深入了解的选项。 - 考虑到Java框架的复杂性和多样性,企业在选择框架时应当综合考虑项目需求、团队技能等因素,以确保选择最适合的工具...
Ruby On Rails 框架自它提出之日起就受到广泛关注,在“不要重复自己”,“约定优于配置”等思想的指导下,Rails 带给 Web 开发者的是极高的开发效率。 ActiveRecord 的灵活让你再也不用配置繁琐的 Hibernate 即可...
Ruby on Rails,简称Rails,是由David Heinemeier Hansson基于Ruby语言开发的一个开源Web应用程序框架。这个框架遵循“约定优于配置”(Convention over Configuration)的原则,致力于简化Web应用的开发流程,提高...