rails是MVC框架,显示Hello,world不需要资料库,我們只要先写Controller和View,以及让路由指派到这个Controller即可。
先到昨天创建的项目demo下:
$cd demo
$ bin/rails g controller welcome --创建一个controller welcome
create app/controllers/welcome_controller.rb
invoke erb
create app/views/welcome
invoke helper
create app/helpers/welcome_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/welcome.coffee
invoke scss
create app/assets/stylesheets/welcome.scss(这些都是rails自动生成的文件)
$ ls 可以看到有下面这些路径文件生成
app bin config config.ru db Gemfile Gemfile~ Gemfile.lock lib log publ
$ gedit config/routes.rb --进到config/routes.rb增加一行:
Rails::Application.routes.draw do
get "welcome/say_hello" => "welcome#say"
# ...
end
get意思是将http://localhost:3000/welcome/say_hello网址对应到welcome Controller的say Action
再去编辑app/controllers/welcome_controller.rb,加入一個say方法:
$ gedit app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
def say
end
end
在Controller中,一个公开函数(public method)就代表一個Action,一個Action对应一个HTTP的请求和回应。接着我们打开浏览器浏览http://localhost:3000/welcome/say_hello,会看到如下错误:
Missing template welcome/say, application/say with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/Users/ihower/projects/demo/app/views"
因为我们没有准备View文件。需要新增appviews/welcomey.html.erb这个文件:
$ gedit app/views/welcome/say.html.erb
<h1>Hello, World!</h1>
这时候在进到浏览器,就能看到Hello, World!了。 第一个hello,world就成功了。
接下来我们新增一个页面并且加入超链接。再次编辑路由文件config/routes.rb加入一個路由:
$ gedit config/routes.rb
Rails::Application.routes.draw do
get "welcome/say_hello" => "welcome#say"
get "welcome" => "welcome#index"
# ...
end
意思是http://localhost:3000/welcome网址对应到welcome Controller的index Action
$ gedit app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
#...
def index
end
end
$appviews/welcome/index.html.erb
<p>Hola! It's <%= Time.now %></p>
<p><%= link_to 'Hello!', welcome_say_hello_path %></p>
Time是Ruby內建的时间,Time.now是输出当前时间。link_to是Rails內建的方法可以输出的超级链接,而welcome_say_hello_path会输出/welcome/say_hello这个网址。这时候去浏览http://localhost:3000/welcome,会看到Hola!以及Hello!超级链接。
接着我们去把网站首页变成welcome#index
$gedit config/routes.rb
Rails::Application.routes.draw do
get "welcome/say_hello" => "welcome#say"
get "welcome" => "welcome#index"
root :to => "welcome#index"
# ...
end
意思是将网站的根目录对应到welcome Controller的index Action。再去编辑View,建立回首頁的链接
$gedit app/views/welcomey.html.erb --在Hello, World!下一行加入:
<h1>Hello, World!</h1>
<p><%= link_to "Home", root_path %></p>
这样网页的首页就会显示Hola!和时间,连到http://localhost:3000/welcome/say_hello的时候,底部也会出现「Home」的链接,点击就会回到首页。
连接数据库:
$getdit config/database.yml --编辑
# SQLite version 3.x
# gem install sqlite3
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
default: &default
adapter: mysql2
host: localhost
usename: root
password: '123456'
pool: 5
timeout: 5000
development:
<<: *default
database: demo_dev
$$ vi Gemfile --编辑一行
gem 'mysql2', '=0.3.13'
$bundle install
$ rake db:create --创建完成即可
$ bin/rails g scaffold person name:string bio:text birthday:date
注意Model的名称是用单数person,而Controller照RESTful惯例是用复数people
invoke active_record
create db/migrate/20151027080428_create_people.rb
create app/models/person.rb
invoke resource_route
route resources :people
invoke scaffold_controller
create app/controllers/people_controller.rb
invoke erb
create appiews/people
create appiews/people/index.html.erb
create appiews/people/edit.html.erb
create appiews/people/show.html.erb
create appiews/people/new.html.erb
create appiews/people/_form.html.erb
invoke helper
create app/helpers/people_helper.rb
invoke jbuilder
create appiews/people/index.json.jbuilder
create appiews/people/show.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/people.coffee
invoke scss
create app/assets/stylesheets/people.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss(ruby自己生成的文件)
可以用gedit命令,查看demo下的config/routes.rb,people的Controller,people的views。
scaffold产生的文件中,有一项是数据库转移(database migration)。Migration的用途是建立和修改数据库表。Rails使用rake指令來执行Migrations。Migration中包含了Timestamp(时间戳),用来确保它们可以依照建立时间按序执行。
$ bin/rake db:migrate --执行Migration
== 20151027080428 CreatePeople: migrating =====================================
-- create_table(:people)
-> 0.0091s
== 20151027080428 CreatePeople: migrated (0.0093s) ============================(Rails建立的的people表)
这时候去浏览器浏览http://localhost:3000/people就可以看见创建的people表。可以做编辑,删除等操作。