A summary of the application basic directory structure built by Ruby on Rails _ruby topics

Source: Internet
Author: User
Tags require ruby on rails

After using the rails new appname to build rails applications, we can view the directory structure of Rails applications through tree:

Directory structure

The application directory will have 11 directories of App, config, DB, Doc, lib, log, public, script, test, TMP, and vendor, and config.ru, Gemfile, Gemfile.lock, Rakefile , Readme.rdoc, etc. 5 documents.

The table of contents will be explained later, look at the files in the app directory first:

config.ru rack settings file used to start the Rails program

Require:: File.expand_path ('.. /config/environment ', __file__)
run myapps::application

Gemfile sets the gems that rails programs rely on (Gemfile.lock will be generated once installed with bundle install)

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

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

# Gems used only for assets and not Requi Red
# 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 used to load rake tasks that can be performed by the terminal
<!--more-->

The following is a view with the tree command, showing the directory and file structure:

. ├──app│├──assets││├──images│││└──rails.png││├──javascripts│││└──application.js││└── Stylesheets││└──application.css│├──controllers││└──application_controller.rb│├──helpers││└──ap
Plication_helper.rb│├──mailers│├──models│└──views│└──layouts│└──application.html.erb├──config │├──application.rb│├──boot.rb│├──database.yml│├──environment.rb│├──environments││├──development.r
B││├──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│├──inde X.html│└──robots.Txt├──rakefile├──readme.rdoc├──script│└──rails├──test│├──fixtures│├──functional│├──integration│ ├──performance││└──browsing_test.rb│├──test_helper.rb│└──unit├──tmp│└──cache│└──assets└──vend
 Or├──assets│├──javascripts│└──stylesheets└──plugins

Application directory (app/)

The app directory is the home directory for the Rails program, with different subdirectories containing the model Models (M), Controller CONTROLLERSW (C), view Views (V) and mailers, helpers, and assests documents.

Model-Controller-View

Store models, controllers, and views separately. Among them, the model is stored uniformly in the App/models directory, the controller is stored in the App/controllers directory (can use the directory to further organize the controller, such as the Cpanel directory for the management of the controller to store the background), the view is stored in the App/views directory , the view model is stored in the App/view/layouts directory, and the default is Applicaiton.html.erb.

Assets static File

Assets static files are stored in the App/assets directory, respectively, App/assets/images, App/assets/stylesheets, app/assets/javascripts directory.

Helper

A helper is a small method that can be used in a view to produce more complex HTML. The preset helper file name corresponds to the controller, but does not enforce the requirement that the method defined in any of the helper files can be used in any view.

Configuration file directory (config/)

While rails follows the principle of "convention over configuration", there are still places to set up. In the configuration file directory, the application settings file application.rb, the database settings file Database.yml, the routing settings file routes.rb, the multiple environment settings config/environments directory, Other initial settings file config/initializers.

Rails launches application settings

Start a rails program (such as rails console or rails server) that performs the following three documents

BOOT.RB load into the bundler environment, this file is automatically generated by rails, no need to modify;

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 Load rails gems and other dependent gems, and then set up rails programs;
Require File.expand_path ('.. /boot ', __file__)

Require ' rails/all ' if defined?  (bundler) # If You are precompile assets before deploying to production, use this line Bundler.require (: *rails.groups =>%w (Development test)) # If You are want your assets, lazily, compiled, use this line # production (: Default,: Assets, rails.env) End Module MyApps class Application < Rails::application # Settings in Config/environm
  ents/* take precedence over those specified here. # application configuration should go to files in Config/initializers # Configure the default encoding US
  Ed in the templates for Ruby 1.9.
  config.encoding = "Utf-8" # Configure sensitive parameters which'll be filtered from the log file. Config.filter_parameters + = [:p assword] # ... # Enable the asset pipeline config.assets.enabled = true # Ve Rsion of your assets, change this if you are want to expire all your assets = ' config.assets.version ' End end 1.0 NT.RB executes all startup programs (initializers), which is also generated by rails, and does notNeed to be modified. # Load The Rails application require File.expand_path ('..

 /application ', __file__) # Initialize the Rails application myapps::application.initialize!

Initial settings file (initializers)
called by ENVIRONMENT.RB, the system default initial settings file has backtrace_silencers.rb, inflections.rb, MIME_TYPES.RB, SECRET_TOKEN.RB, session _STORE.RB and WRAP_PARAMETERS.RB 6, respectively corresponding uses are: Selective movement anomaly tracking, single complex conversion, mime_types, encryption cookies information token, default session storage and parameter encapsulation.

# # #数据库存储目录 (db/)

# # #程序帮助文档 (doc/)

# # #共享类或模块文件 (lib/)

Some shared classes or modules can be stored in the directory. In addition, Rake's task can be stored in the Lib/tasks directory.

# # #日志目录 (log/)

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

For a Web server, a directory of files that can be accessed directly. can be used to store common images, stylesheets, and JavaScripts (Rails 2.x).

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

# # #测试文件目录 (test/)

Used to store unit tests, functional tests, and integration test files.

# # #临时文件目录 (tmp/)

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

When you install the Gems plug-in using Bundler, you can also choose to install it in the directory. For example Bundle install--path Vendor/bundle.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.