rails 目录结构分析 (rails -> 5.1.4)

rails 用  rails new appname  生成rails 应用后,可以通过tree来查看rails应用目录结构

(tree命令的使用可以参考:tree命令的安装及使用

****目录结构****

应用程序下会有 app , bin , config, db , lib , log , public , test, tmp , vendor 10个目录,

和 config.ru ,  Gemfile , package.json, Rakefile, README.md 5个文件

 

~~~  先讲一下APP目录下的文件:

* config.ru 用来启动Rails程序的rack设置文件

# This file is used by Rack-based servers to start the application.

require_relative 'config/environment'

run Rails.application

* Gemfile 设置Rails程序所依赖的Gems(一旦用 bundle install 安装后,会生成Gemfile.lock)

source 'https://gems.ruby-china.org'

gem 'rails', '~> 5.1.4'
gem 'mysql2', '~> 0.4.5'
gem ... ...

group :development, :test do
  gem 'pry-rails', '~> 0.3.4'
  gem 'capistrano', '~> 3.9'
  gem 'capistrano-rails', "~> 1.3.0"
  gem ... ...
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.1.5'
end

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

* Rakefile 用来载入可以被终端执行的Rake 任务

 

用tree命令查看,显示的目录和文件结构:

.
├── Gemfile
├── Gemfile.lock
├── README.md
├── Rakefile
├── app
│   ├── assets
│   │   ├── config
│   │   │   └── manifest.js
│   │   ├── images
│   │   ├── javascripts
│   │   │   ├── application.js
│   │   │   ├── cable.js
│   │   │   └── channels
│   │   └── stylesheets
│   │       └── application.css
│   ├── channels
│   │   └── application_cable
│   │       ├── channel.rb
│   │       └── connection.rb
│   ├── controllers
│   │   ├── application_controller.rb
│   │   └── concerns
│   ├── helpers
│   │   └── application_helper.rb
│   ├── jobs
│   │   └── application_job.rb
│   ├── mailers
│   │   └── application_mailer.rb
│   ├── models
│   │   ├── application_record.rb
│   │   └── concerns
│   └── views
│       └── layouts
│           ├── application.html.erb
│           ├── mailer.html.erb
│           └── mailer.text.erb
├── bin
│   ├── bundle
│   ├── rails
│   ├── rake
│   ├── setup
│   ├── spring
│   ├── update
│   └── yarn
├── config
│   ├── application.rb
│   ├── boot.rb
│   ├── cable.yml
│   ├── database.yml
│   ├── environment.rb
│   ├── environments
│   │   ├── development.rb
│   │   ├── production.rb
│   │   └── test.rb
│   ├── initializers
│   │   ├── application_controller_renderer.rb
│   │   ├── assets.rb
│   │   ├── backtrace_silencers.rb
│   │   ├── cookies_serializer.rb
│   │   ├── filter_parameter_logging.rb
│   │   ├── inflections.rb
│   │   ├── mime_types.rb
│   │   └── wrap_parameters.rb
│   ├── locales
│   │   └── en.yml
│   ├── puma.rb
│   ├── routes.rb
│   ├── secrets.yml
│   └── spring.rb
├── config.ru
├── db
│   └── seeds.rb
├── lib
│   ├── assets
│   └── tasks
├── log
├── package.json
├── public
│   ├── 404.html
│   ├── 422.html
│   ├── 500.html
│   ├── apple-touch-icon-precomposed.png
│   ├── apple-touch-icon.png
│   ├── favicon.ico
│   └── robots.txt
├── test
│   ├── application_system_test_case.rb
│   ├── controllers
│   ├── fixtures
│   │   └── files
│   ├── helpers
│   ├── integration
│   ├── mailers
│   ├── models
│   ├── system
│   └── test_helper.rb
├── tmp
│   └── cache
│       └── assets
└── vendor

42 directories, 58 files

 

 ~~~~  应用目录(app)

###1. app目录是rails程序的主目录,不同子目录下分别存放了 模型Models (M), 控制器 Controllers (C) , 视图 Views (V) 以及 Mailers, Helpers , jobs , Assets , Channels等文档

(1).控制器-模型-视图

# ==============
#  controllers 
# | -- concerns
#      |--
# | -- aaas_controller.rb
# | -- bbbs_controller.rb
# | -- cccs_controller.rb
# | -- admin
#      | --
# | -- api 
#      | --
# | -- web 
#      | --

#  这里记得编写routes.rb (每一次rails g controller controller_name 都要写对应的路由)
#=======================
# ==========================
# models
# | -- concerns
       | --
# | -- aaa.rb
# | -- bbb.rb
# | -- ccc.rb
#==========
#===========================
# views
# | -- admin
#      | -- 
# | -- api
#      | --
# | -- layouts
#      | -- application.html.haml
#      | -- mailer.html.erb
#      | -- mailer.text.erb
# | -- web
#      | --
#===========================

* controller 只组织渲染view的数据

* 如果里面有复杂的逻辑,应该放置在controller的concern中

* 常用的方法应该放在controller的helper中

 

* model组织整个处理当前model的业务逻辑

* 如果有其他相关但是不只是直接操作model的逻辑,应该放在model的concern中

* 如果还有其他既不属于model又不属于controller的东西,应该放在lib/module中,这个地方写你自己的算法啊,或者你想调用的接口啥的,其他东西啥的

(2).helper 

helper是一些视图中可以使用的小方法,用来产生较复杂的HTML,

预设Helper文件名称对应控制器,但不强制要求,在任意一个Helper文件中定义的方法,都可以在任意视图中使用

#=====================
# helpers
# | -- admin
#      | --
# | -- api 
#      | --
# | -- web 
#      | --
# | -- aaas_helper.rb
# | -- bbbs_helper.rb
# | -- cccs_helper.rb

#==================

###2. config -配置文件目录

虽然Rails遵循“约定优于配置”的原则,但仍有一些需要设定的地方。

在配置文件目录下, 

application.rb : 会存放应用程序设置文件 

database.yml : 数据库设置文件 

routes.rb :    路由设置文件

config/environments : 多重环境设置

config/initializers :  目录,其他初始设置文件

 

###3 .Rails 启动应用程序设置

启动Rails程序(rails console 或者 rails server),会执行以下三个文档

· boot.rb  载入Bundler 环境,这个文件由Rails自动产生,不需要修改

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the 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)
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执行所有启动程序(initalizers),这个文件同样由Rails 产生,不需要修改

# Load the rails application
require File.expand_path('../application', __FILE__)

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

· 初始设置文件(initalizers)

由environment.rb调用,系统默认的初始设置文件有6个

backtrace_silencers.rb:选择性移动异常追踪

inflections.rb:单复数转换

mime_types.rb:mime_types

secret_token.rb:加密cookies信息的token

session_store.rb:默认session储存

wrap_parameters.rb:参数封装

 

###数据库存储目录(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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值