子在川上曰

            逝者如斯夫不舍晝夜
          隨筆 - 71, 文章 - 0, 評論 - 915, 引用 - 0
          數(shù)據(jù)加載中……

          Rails學習筆記(4)數(shù)據(jù)庫配置及頁面讀取

          開始在MySQL中創(chuàng)建數(shù)據(jù)庫,分別用于:開發(fā)、測試、產(chǎn)品

          create database chensite_development;
          create database chensite_test;
          create database chensite_production;

          在開發(fā)庫中建表和插入數(shù)據(jù):
          use chensite_development;

          drop table 
          if exists titles;
          create table titles (
           id             
          int           not null auto_increment,
           name           varchar(
          100)  not null,
           url            varchar(
          200)  default NULL,
           parent_id      
          int           default 0,
           expanded       tinyint(
          1)    default 0,
           level          tinyint(
          1)    default 0,
           primary key (id)
          )ENGINE
          =InnoDB DEFAULT CHARSET=utf8;

          LOCK TABLES titles WRITE;
          INSERT INTO titles VALUES(
          1,'AAAAAAAAA','http:\\www.AAAAAAAAA.com.cn',0,0,1);
          INSERT INTO titles VALUES(
          2,'BBBBBBBBB','http:\\www.BBBBBBBBB.com.cn',0,0,1);
          INSERT INTO titles VALUES(
          3,'CCCCCCCCC','http:\\www.CCCCCCCCC.com.cn',0,0,1);
          INSERT INTO titles VALUES(
          4,'關(guān)于','http:\\www.DDDDDDDDD.com.cn',0,0,1);

          INSERT INTO titles VALUES(
          5,'EEEEEEEEE','http:\\www.EEEEEEEEE.com.cn',2,0,2);
          INSERT INTO titles VALUES(
          6,'FFFFFFFFF','http:\\www.FFFFFFFFF.com.cn',2,0,2);
          INSERT INTO titles VALUES(
          7,'GGGGGGGGG','http:\\www.GGGGGGGGG.com.cn',2,0,2);
          INSERT INTO titles VALUES(
          8,'HHHHHHHHH','http:\\www.HHHHHHHHH.com.cn',2,0,2);


          UNLOCK TABLES;

          配置數(shù)據(jù)庫連接:  config\database.yml,主要是給連接三個數(shù)據(jù)庫的root用戶輸入密碼,在輸入密碼時要注意:"password:"和密碼"123456"之間要有一個空格,密碼之后不要有空格,否則無法啟動WEB服務。

          # MySQL (default setup).  Versions 4.1 and 5.0 are recommended.
          #
          # Install the MySQL driver:
          #   gem install mysql
          # On MacOS X:
          #   gem install mysql 
          -- --include=/usr/local/lib
          # On Windows:
          #   gem install mysql
          #       Choose the win32 build.
          #       Install MySQL and put its 
          /bin directory on your path.
          #
          # And be sure to use 
          new-style password hashing:
          #   http:
          //dev.mysql.com/doc/refman/5.0/en/old-client.html
          development:
            adapter: mysql
            database: chensite_development
            username: root
            password: 
          123456
            host: localhost

          # 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: mysql
            database: chensite_test
            username: root
            password: 
          123456
            host: localhost

          production:
            adapter: mysql
            database: chensite_production
            username: root
            password: 
          123456
            host: localhost

          接下來創(chuàng)建一個能夠顯示數(shù)據(jù)庫數(shù)據(jù)的頁面。Rails是MVC模式的編程方式。
          首先創(chuàng)建數(shù)據(jù)模型:app\models\title.rb
          class Title < ActiveRecord::Base
          end
          • 數(shù)據(jù)庫titles(小寫復數(shù)),文件名title.rb(小寫單數(shù)),類名Title(大寫單數(shù))
          • 大寫方式--單詞第一個字母為大寫。小寫方式--每個單詞用下劃線分開。
          • 模型類中不必定義屬性,它會自動以數(shù)據(jù)庫字段為屬性。
          • set_table_name "table1" 定義對應的表
          • set_primary_key "name" 改默認的ID主鍵為name,不過以后name字段就用成id,如:o.id="chengang"

          創(chuàng)建視圖:views\homepage\index.rhtml,顯示出title表所有記錄的id和name值

          <html>
          <body>
              
          <h1>ChenGang's Site</h1>

          <% for title in @titles %>

              
          <%= title.id %>__<%= title.name %><br/>

          <% end %>
          </body>
          </html>

          視圖中用到的@titles變量來自于我們自己創(chuàng)建的如下控制器: app\controllers\homepage_controller.rb。Rails中控制器中的變量可以在視圖中使用(這是否會產(chǎn)生變量污染的問題呢,還待以后再體驗)

          class HomepageController < ApplicationController
            def index
              @titles 
          = Title.find(:all)
            end
          end
          • Homepage和視圖的目錄名homepage相關(guān)
          • index方法和視圖的文件名index.rhtml相關(guān)
          • 用index可以省略訪問地址中的action(action默認為index action)

          最后啟動Web服務器后訪問:http://localhost:3000/homepage 

           

          posted on 2007-04-09 14:36 陳剛 閱讀(2022) 評論(0)  編輯  收藏 所屬分類: Rails&Ruby

          主站蜘蛛池模板: 洛南县| 泰兴市| 漳州市| 太谷县| 霞浦县| 丹巴县| 如皋市| 昭觉县| 黑龙江省| 芒康县| 丰原市| 星子县| 通江县| 盐源县| 开远市| 万盛区| 崇义县| 二连浩特市| 岗巴县| 彝良县| 漳州市| 邮箱| 丘北县| 星子县| 西和县| 张家界市| 邵武市| 个旧市| 康保县| 稻城县| 多伦县| 凭祥市| 宁河县| 威海市| 湾仔区| 巴彦县| 黄冈市| 宜都市| 横山县| 巴中市| 麻江县|