国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Ruby > 正文

Ruby on Rails所構建的應用程序基本目錄結構總結

2020-10-29 19:35:27
字體:
來源:轉載
供稿:網友

當使用rails new appname生成Rails應用后,我們可以通過tree來查看Rails應用的目錄結構:

目錄結構

應用程序目錄下會有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設置文件

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

Gemfile設置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'endgem 'jquery-rails'gem ... ...

Rakefile 用來載入可以被終端執行的Rake任務
<!--more-->

下面是用tree命令查看,所顯示的目錄和文件結構:

.├── 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

應用目錄(app/)

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

模型-控制器-視圖

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

Assets靜態文件

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

Helper

Helper是一些在視圖中可以使用的小方法,用來產生較復雜的HTML。預設的Helper文件名稱對應控制器,但不強制要求,在任意一個Helper文件中定義的方法,都可以在任何視圖中使用。

配置文件目錄(config/)

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

Rails啟動應用程序設置

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

boot.rb 載入Bundler環境,這個文件由Rails自動產生,不需要修改;

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,接著設定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)endmodule 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' endendenvironment.rb 執行所有啟動程序(initializers),這個文件同樣由Rails產生,不需要修改。# Load the rails applicationrequire File.expand_path('../application', __FILE__)# Initialize the rails applicationMyapps::Application.initialize!

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

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

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

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

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

###日志目錄(log/)

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

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

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

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

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

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

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上思县| 潜山县| 巴楚县| 阆中市| 镇平县| 治多县| 乌拉特后旗| 启东市| 泸定县| 延庆县| 张家口市| 顺义区| 平阳县| 鱼台县| 博爱县| 广南县| 常州市| 温宿县| 西林县| 印江| 剑河县| 六安市| 盐亭县| 湘西| 茂名市| 巩留县| 武功县| 合肥市| 黎川县| 太康县| 方正县| 汪清县| 白银市| 阳泉市| 涟水县| 阳信县| 定结县| 藁城市| 鄯善县| 晋中市| 龙口市|