1.Ruby on rails初體驗
接觸ruby on rails 已經有一段時間了,想記錄一下自己的rails歷程。自己寫一些小例子來幫助學習。
Rails 適用於那些以數據爲中心的應用,很多應用的核心部分包括一個數據庫,這些引用的存在的主要原因是爲了讓客戶可以通過它們來操作數據庫,而不是直接使用SQL。
1.1 Rails應用的組成部分:
(1)ActionPack的應用框架。用來負責幫助生成數據驅動的交互頁面來訪問和修改數據庫。
(2)Web服務器。可以是rails內置的web服務器也可以用apache等。
(3)數據庫。可以是rails自帶的sqLite或者是mysql等。
(4)對象-關係映射庫。Rails提供了名爲ActiveRecord的對象-關係映射庫。
(5)Rails的工具腳本工具。例如支架(scaffolding),遷移(migration)。
1.2 軟件環境:
數據庫:postgresql
語言:ruby html
框架:rails
2.用Rails創建Web應用:
2.1在命令行中輸入:
$ rails new OAONLINE
輸出如下信息:
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
create app
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/views/layouts/application.html.erb
create app/assets/images/.keep
create app/mailers/.keep
create app/models/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create config 41 ..............
此時OAONLINE應用就創建成功。
2.2啓動Web應用:
進入OAONLINE程序所在目錄,查看rails爲我們生成的目錄文件。
$ cd OAONLINE/
$ ls -l
total 60
drwxrwxr-x 8 steven steven 4096 Oct 31 21:43 app
drwxr-xr-x 2 steven steven 4096 Oct 31 21:43 bin
drwxrwxr-x 5 steven steven 4096 Oct 31 21:43 config
-rw-rw-r-- 1 steven steven 154 Oct 31 21:43 config.ru
drwxrwxr-x 2 steven steven 4096 Oct 31 21:43 db
-rw-rw-r-- 1 steven steven 1176 Oct 31 21:43 Gemfile
-rw-rw-r-- 1 steven steven 2715 Oct 31 21:44 Gemfile.lock
drwxrwxr-x 4 steven steven 4096 Oct 31 21:43 lib
drwxrwxr-x 2 steven steven 4096 Oct 31 21:43 log
drwxrwxr-x 2 steven steven 4096 Oct 31 21:43 public
-rw-rw-r-- 1 steven steven 253 Oct 31 21:43 Rakefile
-rw-rw-r-- 1 steven steven 478 Oct 31 21:43 README.rdoc
drwxrwxr-x 8 steven steven 4096 Oct 31 21:43 test
drwxrwxr-x 3 steven steven 4096 Oct 31 21:43 tmp
drwxrwxr-x 3 steven steven 4096 Oct 31 21:43 vendor
生成文件的含義,以後在說明。接下來啓動應用程序。
$ rails server
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-10-31 21:52:49] INFO WEBrick 1.3.1
[2013-10-31 21:52:49] INFO ruby 2.0.0 (2013-06-27) [i686-linux]
[2013-10-31 21:52:49] INFO WEBrick::HTTPServer#start: pid=5263 port=3000
應用正常啓動,然後在頁面瀏覽一下,看看程序運行的結果:在瀏覽器中輸入地址: http://0.0.0.0:3000
這樣第一步完成的很順利。
3.接下來定製我們自己的需求
3.1 利用支架生成界面和一些背後的代碼例如路由的設置,控制器的編寫等詳細工作(在後面的章節詳解)
OAONLINE是每個公司可以公用的一個OA系統。滿足每個公司的基本辦公需求。我們需要創建的頁面允許我們創建,讀取,更新,和刪除每個公司的信息。Rails提供一種方式就可以快速生成需要的所有代碼和頁面。scaffolding(支架)。下面先看一下公司有哪些屬性。
下面讓我們見證神奇的時刻。在命令行中輸入如下信息:
$ rails generate scaffold company name:string phone:string address:string email:string fax:string description:text level:decimal
然後產生如下輸出:
invoke active_record
create db/migrate/20131101054917_create_companies.rb
create app/models/company.rb
invoke test_unit
create test/models/company_test.rb
create test/fixtures/companies.yml
invoke resource_route
route resources :companies
invoke scaffold_controller
create app/controllers/companies_controller.rb
invoke erb
create app/views/companies
create app/views/companies/index.html.erb
create app/views/companies/edit.html.erb
create app/views/companies/show.html.erb
create app/views/companies/new.html.erb
create app/views/companies/_form.html.erb
invoke test_unit
create test/controllers/companies_controller_test.rb
invoke helper
create app/helpers/companies_helper.rb
invoke test_unit
create test/helpers/companies_helper_test.rb
invoke jbuilder
create app/views/companies/index.json.jbuilder
create app/views/companies/show.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/companies.js.coffee
invoke scss
create app/assets/stylesheets/companies.css.scss
invoke scss
create app/assets/stylesheets/scaffolds.css.scss
3.2 對應的數據庫
到目前爲止,我們還沒有討論關於數據庫的問題,我們查看一下rails在創建項目的時候爲我們生成的目錄中有個文件夾叫做db,進去看一下
$ ll
total 16
drwxrwxr-x 3 steven steven 4096 Oct 31 22:49 ./
drwxrwxr-x 12 steven steven 4096 Oct 31 21:44 ../
-rw-r--r-- 1 steven steven 0 Oct 31 21:54 development.sqlite3
drwxrwxr-x 2 steven steven 4096 Oct 31 22:49 migrate/
-rw-rw-r-- 1 steven steven 343 Oct 31 21:43 seeds.rb
這是Rails給默認設置的數據庫,我們可以修改,在此將數據庫改爲mysql。修改方式如下:
rails給我們生成的文件中有個config文件夾,裏面有database.yml文件,打開文件,發現裏面有如下一段內容:
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
將development中的內容修改如下:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
development:
adapter: postgresql
database: test
# username: openstreetmap
# password: openstreetmap
# host: localhost
encoding: utf8
# 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: postgresql
database: test
# username: openstreetmap
# password: openstreetmap
# host: localhost
encoding: utf8
production:
adapter: postgresql
database: test
# username: openstreetmap
# password: openstreetmap
# host: localhost
encoding: utf8
注意:如果postgresql數據庫和ruby on rails在同一臺PC機上,則不需要用戶名和密碼驗證登陸
3.3. 增加postgresql數據庫驅動
$vim Gemfile
9 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
10 gem 'rails', '~> 5.1.4'
11 # Use sqlite3 as the database for Active Record
12 gem 'pg'
13 # Use Puma as the app server
14 gem 'puma', '~> 3.7'
15 # Use SCSS for stylesheets
16 gem 'sass-rails', '~> 5.0'
17 # Use Uglifier as compressor for JavaScript assets
18 gem 'uglifier', '>= 1.3.0'
19 # See https://github.com/rails/execjs#readme for more supported runtimes
20 # gem 'therubyracer', platforms: :ruby
$bundler install
...
Using sass-rails 5.0.7
Using selenium-webdriver 3.8.0
Using spring 2.0.2
Using spring-watcher-listen 2.0.1
Using turbolinks-source 5.0.3
Using turbolinks 5.0.1
Using uglifier 4.0.2
Using web-console 3.5.1
Bundle complete! 16 Gemfile dependencies, 70 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
3.4 創建數據庫
$rake db:create
Created database 'test'
Database 'test' already exists
3.4. 通過遷移來創建數據表
$rake db:migrate
== 20171215021556 CreateCompanies: migrating ==================================
-- create_table(:companies)
-> 0.0127s
== 20171215021556 CreateCompanies: migrated (0.0128s) =========================
3.5 瀏覽器預覽
下面看一下咱們的工作成果:在瀏覽器中輸入如下地址:http://0.0.0.0:3000/companies
如下爲我們生成的幾個頁面:
OK。大概就是這個樣子,實在希望大家幫我指點一下。多多交流。