Ror Creating a project Flow

Source: Internet
Author: User
Tags datetime php framework sqlite sqlite database touch

Getting Started with Rails

One months ago without any knowledge of rails, the starter project has been successfully built to demonstrate the simplicity of rails on Ruby for web development, and after one months of learning to have a certain understanding of rails, try to build it over the weekend to increase your awareness. What's more, we'll see where it's convenient to develop with rails. Environmental Information

The various tools required to install the environment by default

Create a new project

A good framework, when creating a project, will automatically initialize some common files, do not need us to add manually. Let's see how rails is created:

➜developer Rails New Blog Create create readme.md create Rakefile create config.ru Crea Te. Gitignore Create gemfile Create app create app/assets/config/manifest.js create App/asset S/javascripts/application.js Create app/assets/javascripts/cable.js Create App/assets/stylesheets/applicatio
      N.CSS Create app/channels/application_cable/channel.rb Create app/channels/application_cable/connection.rb Create APP/CONTROLLERS/APPLICATION_CONTROLLER.RB Create app/helpers/application_helper.rb create app/j
      OBS/APPLICATION_JOB.RB Create app/mailers/application_mailer.rb Create app/models/application_record.rb Create App/views/layouts/application.html.erb Create App/views/layouts/mailer.html.erb create app/views/l Ayouts/mailer.text.erb Create App/assets/images/.keep Create app/assets/javascripts/channels Create a Pp/assets/javascripts/chAnnels/.keep Create app/controllers/concerns/.keep Create app/models/concerns/.keep Create Bin Create Bin/bundle Create bin/rails Create Bin/rake create bin/setup create Bin/update C
      reate config Create config/routes.rb create config/application.rb create config/environment.rb
      Create config/secrets.yml Create config/cable.yml create config/puma.rb create config/spring.rb Create config/environments Create config/environments/development.rb create Config/environments/producti ON.RB Create config/environments/test.rb Create config/initializers create Config/initializers/applic ATION_CONTROLLER_RENDERER.RB Create config/initializers/assets.rb Create Config/initializers/backtrace_silen CERS.RB Create CONFIG/INITIALIZERS/COOKIES_SERIALIZER.RB Create CONFIG/INITIALIZERS/CORS.RB create Co Nfig/initializers/filter_PARAMETER_LOGGING.RB Create config/initializers/inflections.rb Create config/initializers/mime_types.rb  Create CONFIG/INITIALIZERS/NEW_FRAMEWORK_DEFAULTS.RB Create CONFIG/INITIALIZERS/SESSION_STORE.RB Create CONFIG/INITIALIZERS/WRAP_PARAMETERS.RB Create config/locales Create config/locales/en.yml Create conf IG/BOOT.RB Create CONFIG/DATABASE.YML Create db Create db/seeds.rb create lib create Li B/tasks Create lib/tasks/.keep Create lib/assets create lib/assets/.keep create log Crea Te log/.keep Create public create public/404.html create public/422.html create public/500.ht ML Create public/apple-touch-icon-precomposed.png Create public/apple-touch-icon.png create Public/fa Vicon.ico Create public/robots.txt Create test/fixtures create test/fixtures/.keep Create test /fixtures/files CREate test/fixtures/files/.keep Create test/controllers create test/controllers/.keep create Test/mai 
      Lers Create test/mailers/.keep Create test/models create test/models/.keep create Test/helpers Create Test/helpers/.keep Create test/integration create test/integration/.keep create test/t EST_HELPER.RB Create TMP Create tmp/.keep create Tmp/cache create Tmp/cache/assets creat E vendor/assets/javascripts Create vendor/assets/javascripts/.keep create Vendor/assets/stylesheets C  reate vendor/assets/stylesheets/.keep Remove CONFIG/INITIALIZERS/CORS.RB Run bundle install fetching gem Metadata from https://rubygems.org/Fetching version metadata from https://rubygems.org/Fetching dependency metadata fro
M https://rubygems.org/Resolving dependencies ..... Using Rake 11.2.2 using Concurrent-ruby 1.0.2 using i18n 0.7.0 using minitest 5.9.0 using threAd_safe 0.3.5 using builder 3.2.2 using Erubis 2.7.0 using Mini_portile2 2.1.0 using pkg-config 1.1.7 using rack 2.0.1 Usi ng nio4r 1.2.1 using websocket-extensions 0.1.2 using Mime-types-data 3.2016.0521 Your user account isn ' t allowed to INS
  Tall to the system Rubygems. You can cancel this installation and Run:bundle install--path vendor/bundle to install the gems into./vendor/

  bundle/, or you can enter your password and install the bundled gems to Rubygems using sudo. Password:installing Arel 7.1.1 using bundler 1.12.5 using Byebug 9.0.5 using Coffee-script-source 1.10.0 using EXECJS 2.7 .0 using Method_source 0.8.2 using Thor 0.19.1 using debug_inspector 0.0.2 installing FFI 1.9.14 with native extensions Us ing Multi_json 1.12.1 using rb-fsevent 0.9.7 installing Puma 3.6.0 with native extensions using SASS 3.4.22 using Tilt 2.0 .5 using Spring 1.7.2 using sqlite3 1.3.11 using Turbolinks-source 5.0.0 using Tzinfo 1.2.2 using Nokogiri 1.6.8 using RAC K-test 0.6.3 UsinG sprockets 3.7.0 using Websocket-driver 0.6.4 using Mime-types 3.1 using Coffee-script 2.4.1 installing Uglifier 3.0.1 Us ing rb-inotify 0.9.7 installing turbolinks 5.0.1 using Activesupport 5.0.0 using Loofah 2.0.3 using mail 2.6.4 using Liste n 3.0.8 using rails-dom-testing 2.0.1 installing Globalid 0.3.7 using Activemodel 5.0.0 using JBuilder 2.6.0 using rails-h Tml-sanitizer 1.0.3 using Spring-watcher-listen 2.0.0 using Activejob 5.0.0 using ActiveRecord 5.0.0 using Actionview 5.0. 
0 using Actionpack 5.0.0 using actioncable 5.0.0 using Actionmailer 5.0.0 using railties 5.0.0 using Sprockets-rails 3.1.1  Using coffee-rails 4.2.1 using jquery-rails 4.1.1 using Web-console 3.3.1 using Rails 5.0.0 using Sass-rails 5.0.6 Bundle complete!
Gemfile dependencies, Gems now installed.
         Use ' bundle show [Gemname] ' to see where a bundled gem is installed. Run bundle EXEC Spring binstub--all * bin/rake:spring inserted * bin/rails:spring inserted

This command has already created all the project directory files, and installed all the dependencies, think back to the process of creating the project, first create the project, then add the dependent jar package, at least two commands, so look at the rails on Ruby to know how much more gradle. Contents

➜blog Tree. ├──gemfile├──gemfile.lock├──readme.md├──rakefile├──app│├──assets││├──config│││└──manifest . Js││├──images││├──javascripts│││├──application.js│││├──cable.js│││└──cha Nnels││└──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│└──lay outs│├──application.html.erb│├──mailer.html.erb│└──mailer.text.erb├──bin│├── Bundle│├──rails│├──rake│├──setup│├──spring│└──update├──config│├──application.rb│├──bo Ot.rb│  ├──cable.yml│├──database.yml│├──environment.rb│├──environments││├──development.rb││├──pr
Oduction.rb││└──test.rb│├──initializers││├──application_controller_renderer.rb││├──assets.rb ││├──backtrace_silencers.rb││├──cookies_serializer.rb││├──filter_parameter_logging.rb││├──i Nflections.rb││├──mime_types.rb││├──new_framework_defaults.rb││├──session_store.rb││└──wra 
P_parameters.rb│├──locales││└──en.yml│├──puma.rb│├──routes.rb│├──secrets.yml│└──spring.rb ├──config.ru├──db│└──seeds.rb├──lib│├──assets│└──tasks├──log├──public│├──404.html│├──42 2.html│├──500.html│├──apple-touch-icon-precomposed.png│├──apple-touch-icon.png│├──favicon.ico│└── Robots.txt├──test│├──controllers│├──fixtures││└──files│├──helpers│├──integration│├──mai   Lers│├──models│└──test_helper.rb├──tmp│└──cache│└──assets└──vendor└──assets├──javascripts└── stylesheets directories, the files

The initialized project contains 44 directories and 57 files, explained in detail in the reference document launch

After one of the above commands our HelloWorld project has been created and is now launched

➜  Blog Rails s
= booting Puma
= Rails 5.0.0 application starting in development on HTTP://LOCALHOST:30 xx +
Run ' rails server-h ' For more startup options
Puma starting in single mode
... * Version 3.6.0 (Ruby 2.3.0-p0), codename:sleepy Sunday Serenity
* Min threads:5, max threads:5
* Environment: Development
* Listening on tcp://localhost:3000 use
ctrl-c to stop

Just such a project was created and started successfully. is not so easy! Add Controller

I am currently doing PHP development, PHP framework to create a controller is this, first in the controller's main file, add a route. Then add an action to handle the request in the actions, then create a page and data in the model and a DAO, these are the manual creation process, and you must define the naming specification according to the requirements of other people's framework, since the framework has been required to standardize your naming, So why not create the framework directly?

So, rails does it, and see how rails can do all of the above with a single command:

➜  Blog Rails generate controller welcome index
Running via Spring preloader in process 70093
      create  app/c ONTROLLERS/WELCOME_CONTROLLER.RB
       Route  get ' welcome/index '
      invoke  erb
      create    app/views/ Welcome
      Create    App/views/welcome/index.html.erb
      invoke  test_unit
      create    test/ CONTROLLERS/WELCOME_CONTROLLER_TEST.RB
      Invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke    Test_unit
      invoke  assets
      invoke    coffee
      Create      app/assets/ Javascripts/welcome.coffee
      invoke    scss
      create      app/assets/stylesheets/welcome.scss

Look at the number of files created by this command: Controller file: welcome_controller.rb route add Welcome/index,get Request Add welcome folder and HTML file in view Index.html.erb Single test file welcome_controller_test.rb Help file welcome_helper.rb js file and CSS File

This function is a good interpretation of the Convention better than the configuration.

Let's continue to the homepage settings

Add the following statement in the CONFIG/ROUTES.RB:

Root ' Welcome#index '

Root this keyword is defined the root page of the page, the above usage is to tell the server, to find welcome this controller's index method, see the following welcome_controller.rb file, index method nothing ah,

This is another convention, the Convention says to jump to Welcome/index.html.erb page by default

Now let's visit localhost:3000:

It's that simple. Route View

To know how much routing information we have, you can view it through the rake routes command

➜  Blog Rake routes
       Prefix Verb URI Pattern              controller#action
         root GET  /                        welcome#index
Welcome_index GET  /welcome/index (.: format) Welcome#index

And then we'll make another pact. Add a row to the CONFIG/ROUTES.RB because of the configuration

Resources:articles

This time we'll see how many routes there are:

➜  Blog Rake routes
       Prefix Verb   URI Pattern                  controller#action
     articles GET    /articles (.: Format)          articles#index
              POST   /articles (.: format)          articles#create
  new_article GET    / Articles/new (.: format)      articles#new
 edit_article GET    /articles/:id/edit (.: format) Articles#edit
      article GET    /articles/:id (.: format)      articles#show
              PATCH  /articles/:id (.: format)      Articles#update
              PUT    /articles/:id (.: format)      articles#update
              delete/articles/:id (.: format)      Articles#destroy
         root get    /                            welcome#index
welcome_index get    /welcome/index (.: Format)     Welcome#index

The extra ones are created by default. Create a model

➜  Blog Rails generate model article
Running via Spring preloader in process 79757
      invoke  active_record< C4/>create    db/migrate/20160809092044_create_articles.rb
      Create    app/models/article.rb
      invoke    test_unit
      Create      test/models/article_test.rb
      Create      test/fixtures/articles.yml

A migration file was generated, identified by a timestamp
and model file article.rb
Test file: test/models/article_test.rb
Test simulation Data file: Test/fixtures/articles.yml performing migration

➜  Blog Rake db:migrate
= = 20160809092501 createarticles:migrating ===================================
--create_table (: articles),
   0.0012s
= = 20160809092501 createarticles:migrated (0.0013s) ======= ===================

The default SQLite database, we go to look at the database table.

Database-related information is defined in the CONF/DATABASE.YML

Default: &default
  adapter:sqlite3
  pool:5
  timeout:5000

development:
  <<: *default
  database:db/development.sqlite3

# warning:the database defined as "test" would be erased and
# re-generated From your development The database when you run "rake".
# do not set this db to the same as development or production.
Test:
  <<: *default
  database:db/test.sqlite3

production:
  <<: *default
  Database:db/production.sqlite3

We can see the database of our development environment as DB/DEVELOPMENT.SQLITE3, let's go inside to see:

➜  Blog sqlite3 db/development.sqlite3
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ". Help" for Usage hints.
sqlite>. Tables
ar_internal_metadata  articles              schema_migrations
sqlite>. Schema Articles
CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY autoincrement not NULL, "Created_at" datetime is NOT NULL, "U Pdated_at "datetime not NULL);

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.