當(dāng)使用rails new appname生成Rails應(yīng)用后,我們可以通過(guò)tree來(lái)查看Rails應(yīng)用的目錄結(jié)構(gòu):
目錄結(jié)構(gòu)
應(yīng)用程序目錄下會(huì)有app、config、db、doc、lib、log、public、script、test、tmp和vendor等11個(gè)目錄和config.ru、Gemfile、Gemfile.lock、Rakefile、README.rdoc等5個(gè)文件。
目錄在稍后會(huì)一一解釋?zhuān)瓤匆幌耡pp目錄下的文件:
config.ru 用來(lái)啟動(dòng)Rails程序的Rack設(shè)置文件
require ::File.expand_path('../config/environment', __FILE__) run Myapps::Application
Gemfile設(shè)置Rails程序所依賴(lài)的Gems (一旦用bundle install安裝后,會(huì)生成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 用來(lái)載入可以被終端執(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目錄下用于存放管理后臺(tái)相關(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是一些在視圖中可以使用的小方法,用來(lái)產(chǎn)生較復(fù)雜的HTML。預(yù)設(shè)的Helper文件名稱(chēng)對(duì)應(yīng)控制器,但不強(qiáng)制要求,在任意一個(gè)Helper文件中定義的方法,都可以在任何視圖中使用。
配置文件目錄(config/)
雖然Rails遵循“約定優(yōu)于配置”的原則,但仍有一些需要設(shè)定的地方。在配置文件目錄下,會(huì)存放應(yīng)用程序設(shè)置文件application.rb、數(shù)據(jù)庫(kù)設(shè)置文件database.yml、路由設(shè)置文件routes.rb、多重環(huán)境設(shè)置config/environments目錄、其它初始設(shè)置文件config/initializers。
Rails啟動(dòng)應(yīng)用程序設(shè)置
啟動(dòng)Rails程序(例如rails console或rails server),會(huì)執(zhí)行以下三個(gè)文檔
boot.rb 載入Bundler環(huán)境,這個(gè)文件由Rails自動(dòng)產(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和依賴(lài)的其它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í)行所有啟動(dòng)程序(initializers),這個(gè)文件同樣由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個(gè),分別對(duì)應(yīng)的用途是:選擇性移動(dòng)異常追蹤、單復(fù)數(shù)轉(zhuǎn)換、mime_types、加密cookies信息的token、默認(rèn)session儲(chǔ)存以及參數(shù)封裝等。
###數(shù)據(jù)庫(kù)存儲(chǔ)目錄(db/)
###程序幫助文檔(doc/)
###共享類(lèi)或模塊文件(lib/)
一些共享的類(lèi)或模塊可以存放在該目錄。另外,Rake的任務(wù),可存放在lib/tasks目錄下。
###日志目錄(log/)
###公共文件目錄(public/)
對(duì)于web服務(wù)器來(lái)說(shuō),可以直接訪問(wèn)的文件目錄??梢杂糜诖娣磐ㄓ玫膇mages、stylesheets和javascripts (Rails 2.x)。
###Rails腳本文件(script/)
###測(cè)試文件目錄(test/)
用于存放單元測(cè)試、功能測(cè)試及整合測(cè)試文件。
###臨時(shí)文件目錄(tmp/)
###第三方插件目錄(vendor/)
在使用bundler安裝gems插件時(shí),也可以選擇安裝在該目錄下。例如bundle install --path vendor/bundle。
標(biāo)簽:普洱 永州 張家界 遼寧 荊門(mén) 公主嶺 梧州 三沙
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby on Rails所構(gòu)建的應(yīng)用程序基本目錄結(jié)構(gòu)總結(jié)》,本文關(guān)鍵詞 Ruby,Rails,所,構(gòu)建,的,應(yīng)用程序,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。