佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Ruby on Rails所構(gòu)建的應(yīng)用程序基本目錄結(jié)構(gòu)總結(jié)

Ruby on Rails所構(gòu)建的應(yīng)用程序基本目錄結(jié)構(gòu)總結(jié)

熱門標(biāo)簽:Win7旗艦版 百度AI接口 企業(yè)做大做強(qiáng) 客戶服務(wù) 電話運(yùn)營中心 呼叫中心市場需求 語音系統(tǒng) 硅谷的囚徒呼叫中心

當(dāng)使用rails new appname生成Rails應(yīng)用后,我們可以通過tree來查看Rails應(yīng)用的目錄結(jié)構(gòu):

目錄結(jié)構(gòu)

應(yīng)用程序目錄下會有app、config、db、doc、lib、log、public、script、test、tmp和vendor等11個目錄和config.ru、Gemfile、Gemfile.lock、Rakefile、README.rdoc等5個文件。

目錄在稍后會一一解釋,先看一下app目錄下的文件:

config.ru 用來啟動Rails程序的Rack設(shè)置文件

require ::File.expand_path('../config/environment', __FILE__)
run Myapps::Application

Gemfile設(shè)置Rails程序所依賴的Gems (一旦用bundle install安裝后,會生成Gemfile.lock)

source 'https://ruby.taobao.org/'

gem 'rails', '3.2.1'
gem 'sqlite3'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
 gem 'sass-rails',  '~> 3.2.3'
 gem 'coffee-rails', '~> 3.2.1'
 gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem ... ...

Rakefile 用來載入可以被終端執(zhí)行的Rake任務(wù)
!--more-->

下面是用tree命令查看,所顯示的目錄和文件結(jié)構(gòu):

.
├── app
│  ├── assets
│  │  ├── images
│  │  │  └── rails.png
│  │  ├── javascripts
│  │  │  └── application.js
│  │  └── stylesheets
│  │    └── application.css
│  ├── controllers
│  │  └── application_controller.rb
│  ├── helpers
│  │  └── application_helper.rb
│  ├── mailers
│  ├── models
│  └── views
│    └── layouts
│      └── application.html.erb
├── config
│  ├── application.rb
│  ├── boot.rb
│  ├── database.yml
│  ├── environment.rb
│  ├── environments
│  │  ├── development.rb
│  │  ├── production.rb
│  │  └── test.rb
│  ├── initializers
│  │  ├── backtrace_silencers.rb
│  │  ├── inflections.rb
│  │  ├── mime_types.rb
│  │  ├── secret_token.rb
│  │  ├── session_store.rb
│  │  └── wrap_parameters.rb
│  ├── locales
│  │  └── en.yml
│  └── routes.rb
├── config.ru
├── db
│  └── seeds.rb
├── doc
│  └── README_FOR_APP
├── Gemfile
├── lib
│  ├── assets
│  └── tasks
├── log
├── public
│  ├── 404.html
│  ├── 422.html
│  ├── 500.html
│  ├── favicon.ico
│  ├── index.html
│  └── robots.txt
├── Rakefile
├── README.rdoc
├── script
│  └── rails
├── test
│  ├── fixtures
│  ├── functional
│  ├── integration
│  ├── performance
│  │  └── browsing_test.rb
│  ├── test_helper.rb
│  └── unit
├── tmp
│  └── cache
│    └── assets
└── vendor
  ├── assets
  │  ├── javascripts
  │  └── stylesheets
  └── plugins

應(yīng)用目錄(app/)

app目錄是Rails程序的主目錄,不同子目錄分別存放了模型 Models (M)、控制器 Controllersw (C)、視圖 Views (V)及Mailers、Helpers和Assests等文檔。

模型-控制器-視圖

分別存放模型、控制器和視圖。其中,模型統(tǒng)一存放在app/models目錄下,控制器統(tǒng)一存放在app/controllers目錄下(可以使用目錄進(jìn)一步組織控制器,例如cpanel目錄下用于存放管理后臺相關(guān)的控制器),視圖存放在app/views目錄下,視圖模型存放在app/view/layouts目錄下,默認(rèn)為applicaiton.html.erb。

Assets靜態(tài)文件

Assets靜態(tài)文件存放在app/assets目錄下,分別為app/assets/images、app/assets/stylesheets、app/assets/javascripts目錄。

Helper

Helper是一些在視圖中可以使用的小方法,用來產(chǎn)生較復(fù)雜的HTML。預(yù)設(shè)的Helper文件名稱對應(yīng)控制器,但不強(qiáng)制要求,在任意一個Helper文件中定義的方法,都可以在任何視圖中使用。

配置文件目錄(config/)

雖然Rails遵循“約定優(yōu)于配置”的原則,但仍有一些需要設(shè)定的地方。在配置文件目錄下,會存放應(yīng)用程序設(shè)置文件application.rb、數(shù)據(jù)庫設(shè)置文件database.yml、路由設(shè)置文件routes.rb、多重環(huán)境設(shè)置config/environments目錄、其它初始設(shè)置文件config/initializers。

Rails啟動應(yīng)用程序設(shè)置

啟動Rails程序(例如rails console或rails server),會執(zhí)行以下三個文檔

boot.rb 載入Bundler環(huán)境,這個文件由Rails自動產(chǎn)生,不需要修改;

require 'rubygems'

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)

require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

application.rb 載入Rails gems和依賴的其它gems,接著設(shè)定Rails程序;
require File.expand_path('../boot', __FILE__)

require 'rails/all'

if defined?(Bundler)
 # If you precompile assets before deploying to production, use this line
 Bundler.require(*Rails.groups(:assets => %w(development test)))
 # If you want your assets lazily compiled in production, use this line
 # Bundler.require(:default, :assets, Rails.env)
end

module Myapps
 class Application  Rails::Application
  # Settings in config/environments/* take precedence over those specified here.
  # Application configuration should go into files in config/initializers
  # ... ...

  # Configure the default encoding used in templates for Ruby 1.9.
  config.encoding = "utf-8"

  # Configure sensitive parameters which will be filtered from the log file.
  config.filter_parameters += [:password]

  # ... ...

  # Enable the asset pipeline
  config.assets.enabled = true

  # Version of your assets, change this if you want to expire all your assets
  config.assets.version = '1.0'
 end
end
environment.rb 執(zhí)行所有啟動程序(initializers),這個文件同樣由Rails產(chǎn)生,不需要修改。
# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
Myapps::Application.initialize!

初始設(shè)置文件(initializers)
由environment.rb調(diào)用,系統(tǒng)默認(rèn)的初始設(shè)置文件有backtrace_silencers.rb、inflections.rb、mime_types.rb、secret_token.rb、session_store.rb和wrap_parameters.rb等6個,分別對應(yīng)的用途是:選擇性移動異常追蹤、單復(fù)數(shù)轉(zhuǎn)換、mime_types、加密cookies信息的token、默認(rèn)session儲存以及參數(shù)封裝等。

###數(shù)據(jù)庫存儲目錄(db/)

###程序幫助文檔(doc/)

###共享類或模塊文件(lib/)

一些共享的類或模塊可以存放在該目錄。另外,Rake的任務(wù),可存放在lib/tasks目錄下。

###日志目錄(log/)

###公共文件目錄(public/)

對于web服務(wù)器來說,可以直接訪問的文件目錄。可以用于存放通用的images、stylesheets和javascripts (Rails 2.x)。

###Rails腳本文件(script/)

###測試文件目錄(test/)

用于存放單元測試、功能測試及整合測試文件。

###臨時文件目錄(tmp/)

###第三方插件目錄(vendor/)

在使用bundler安裝gems插件時,也可以選擇安裝在該目錄下。例如bundle install --path vendor/bundle。

您可能感興趣的文章:
  • Ruby on Rails中MVC結(jié)構(gòu)的數(shù)據(jù)傳遞解析

標(biāo)簽:濟(jì)南 長沙 海南 喀什 崇左 山西 山西 安康

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby on Rails所構(gòu)建的應(yīng)用程序基本目錄結(jié)構(gòu)總結(jié)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    西城区| 武鸣县| 嘉峪关市| 宜川县| 扎囊县| 滦平县| 叙永县| 舒城县| 文化| 荥阳市| 延长县| 涡阳县| 玉树县| 无锡市| 岐山县| 定边县| 平湖市| 广元市| 雅江县| 思茅市| 鹿泉市| 阳谷县| 安仁县| 临邑县| 万安县| 安溪县| 颍上县| 固安县| 铜川市| 广元市| 扬中市| 玉门市| 弋阳县| 彭山县| 扬州市| 冷水江市| 滦南县| 金寨县| 闽清县| 邵东县| 丰顺县|