学习页面的 原地址: http://guides.rubyonrails.org/getting_started.html
中文翻译页面: http://guides.ruby-china.org/getting_started.html
这是第二篇. 第一篇 写到了 更改index文件 . 进入主页 http://0.0.0.0:3000 后 能够显示 hello,rails .
数据库
每个Rails应用都会与一个数据库交互. 数据库配置文件: config/database.yml
如果打开一个新rails应用程序配置文件, 你将会看到默认的数据库被配置使用SQLite3. 这个文件包括rrails默认能够运行的三个不同环境部分:
development
test
production
配置一个SQLite3数据库
Rails内置支持SQLite, 轻量级非服务器(serverless)数据库应用程序. 下面是 database.yml 文件
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
创建资源
可以使用scaffolded 产生post源. 表现为一个简单的blog posting. 在终端输入
localhost:blog staticor$ rails generate scaffold Post name:string title:string content:text
创建器可以自动在应用程序中生成一些文件, 且编辑 config/routes.rb 文件大概说明
File/Folder | Purpose |
---|---|
db/migrate/20100207214725_create_posts.rb | 将创建的posts表单迁移到你的数据库(会在你的命名前面加上时间) |
app/models/post.rb | Post模型 |
test/unit/post_test.rb | Unit testing harness for the posts model |
test/fixtures/posts.yml | 模拟测试post |
config/routes.rb | Edited to include routing information for posts |
app/controllers/posts_controller.rb | The Posts controller |
app/views/posts/index.html.erb | 一个显示所有post的视图 |
app/views/posts/edit.html.erb | 一个编辑post的视图 |
app/views/posts/show.html.erb | 一个显示一条post的视图 |
app/views/posts/new.html.erb | 一个创建post的视图 |
app/views/posts/_form.html.erb | 一个局部用于控制编辑和创建新视图的整体视效的表单 |
test/functional/posts_controller_test.rb | Functional testing harness for the posts controller |
app/helpers/posts_helper.rb | 使用post的helper |
test/unit/helpers/posts_helper_test.rb | Unit testing harness for the posts helper |
app/assets/javascripts/posts.js.coffee | CoffeeScript for the posts controller |
app/assets/stylesheets/posts.css.scss | Cascading style sheet for the posts controller |
app/assets/stylesheets/scaffolds.css.scss | Cascading style sheet#层叠样式 to make the scaffolded views look better |
即便是scaffolding使你创建和运行非常快捷,但是产生的代码不可能完美的适合你的应用程序。你大多数都需要定制产生的代码。很多有经验的Rails开发人员完全不使用scaffolding,宁愿从头编写全部的代码。Rails,无论如何,使得为生成的models,controllers,views或者其他代码编定制模板非常简单。你可以在
Creating and Customizing Rails Generators & Templates
看到更多信息。
数据迁移
rails generate scaffold 的一个产物是数据迁移. Migrations是ruby类. 设计用来简化对数据库表单的创建和修改.
Rails中使用rake来做迁移.
迁移文件包含一个时间戳 确保完成.
example:
如果你想看 db/migrate/20100302124523_create_posts.rb
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :name
t.string :title
t.stringtext :content
t.timestamps
end
end
end
可以看到 migration 创建了一个change方法. Posts的几个字段名 string, string stringtext
可以使用rake命令运行migration :
rake db:migrate
|
Rails 将会执行这个 migration 命令并且通知你它创建了 Post 表单。
== CreatePosts: migrating ====================================================
-- create_table(:posts)
-> 0.0019s
== CreatePosts: migrated (0.0020s) ===========================================
|
由于你默认工作在开发环境中,这个命令将会应用于开发环境会话的数据库位于你的config/database.yml 中。如果你想执行 migration 在其他环境中,比如以产品(环境)为实例,你必须明确调用的通过命令行中执行:rake db:migrate RAILS_ENV=production。
..d
Working with Posts in the Browser
在主页添加一个link.
app/views/home/index.html.erb
添加一个Link:
<%= link_to "Myblog", posts_path %>
Model
app/models/post.rb