Ruby on Rails のチュートリアル → Getting Started with Rails — Ruby on Rails Guides

Ruby on Rails のインストール。


$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin16]

$ gem install rails

$ rails --version
Rails 5.1.5

blog というアプリケーションを生成する。

チュートリアルにはソースコードを記述するところも載っているが、今回はコード生成する等のコマンドだけを打っていく。


$ rails new blog
$ cd blog
$ bin/rails server
$ bin/rails generate controller Welcome index
$ bin/rails generate controller Articles
$ bin/rails generate model Article title:string text:text
$ bin/rails generate model Comment commenter:string body:text article:references
$ bin/rails generate controller Comments

どんなファイルが生成されたか ls と tree コマンドで確認。


$ ls -1
Gemfile
Gemfile.lock
README.md
Rakefile
app
bin
config
config.ru
db
lib
log
package.json
public
test
tmp
vendor

$ tree
.
├── Gemfile
├── Gemfile.lock
├── README.md
├── Rakefile
├── app
│   ├── assets
│   │   ├── config
│   │   │   └── manifest.js
│   │   ├── images
│   │   ├── javascripts
│   │   │   ├── application.js
│   │   │   ├── articles.coffee
│   │   │   ├── cable.js
│   │   │   ├── channels
│   │   │   ├── comments.coffee
│   │   │   └── welcome.coffee
│   │   └── stylesheets
│   │       ├── application.css
│   │       ├── articles.scss
│   │       ├── comments.scss
│   │       └── welcome.scss
│   ├── channels
│   │   └── application_cable
│   │       ├── channel.rb
│   │       └── connection.rb
│   ├── controllers
│   │   ├── application_controller.rb
│   │   ├── articles_controller.rb
│   │   ├── comments_controller.rb
│   │   ├── concerns
│   │   └── welcome_controller.rb
│   ├── helpers
│   │   ├── application_helper.rb
│   │   ├── articles_helper.rb
│   │   ├── comments_helper.rb
│   │   └── welcome_helper.rb
│   ├── jobs
│   │   └── application_job.rb
│   ├── mailers
│   │   └── application_mailer.rb
│   ├── models
│   │   ├── application_record.rb
│   │   ├── article.rb
│   │   ├── comment.rb
│   │   └── concerns
│   └── views
│       ├── articles
│       ├── comments
│       ├── layouts
│       │   ├── application.html.erb
│       │   ├── mailer.html.erb
│       │   └── mailer.text.erb
│       └── welcome
│           └── index.html.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
│   ├── migrate
│   │   ├── 20180320140857_create_articles.rb
│   │   └── 20180320140956_create_comments.rb
│   └── seeds.rb
├── lib
│   ├── assets
│   └── tasks
├── log
│   └── development.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
│   │   ├── articles_controller_test.rb
│   │   ├── comments_controller_test.rb
│   │   └── welcome_controller_test.rb
│   ├── fixtures
│   │   ├── articles.yml
│   │   ├── comments.yml
│   │   └── files
│   ├── helpers
│   ├── integration
│   ├── mailers
│   ├── models
│   │   ├── article_test.rb
│   │   └── comment_test.rb
│   ├── system
│   └── test_helper.rb
├── tmp
│   ├── cache
│   │   └── assets
│   ├── pids
│   ├── restart.txt
│   └── sockets
└── vendor

48 directories, 84 files

どういう module が生成されているか確認。


$ grep -r --include=*.rb module .
./app/channels/application_cable/channel.rb:module ApplicationCable
./app/channels/application_cable/connection.rb:module ApplicationCable
./app/helpers/application_helper.rb:module ApplicationHelper
./app/helpers/articles_helper.rb:module ArticlesHelper
./app/helpers/comments_helper.rb:module CommentsHelper
./app/helpers/welcome_helper.rb:module WelcomeHelper
./config/application.rb:module Blog
./config/initializers/assets.rb:# Add Yarn node_modules folder to the asset load path.
./config/initializers/assets.rb:Rails.application.config.assets.paths << Rails.root.join('node_modules')

どういう class が生成されているか確認。


$ grep -r --include=*.rb class .
./app/channels/application_cable/channel.rb:  class Channel < ActionCable::Channel::Base
./app/channels/application_cable/connection.rb:  class Connection < ActionCable::Connection::Base
./app/controllers/application_controller.rb:class ApplicationController < ActionController::Base
./app/controllers/articles_controller.rb:class ArticlesController < ApplicationController
./app/controllers/comments_controller.rb:class CommentsController < ApplicationController
./app/controllers/welcome_controller.rb:class WelcomeController < ApplicationController
./app/jobs/application_job.rb:class ApplicationJob < ActiveJob::Base
./app/mailers/application_mailer.rb:class ApplicationMailer < ActionMailer::Base
./app/models/application_record.rb:class ApplicationRecord < ActiveRecord::Base
./app/models/application_record.rb:  self.abstract_class = true
./app/models/article.rb:class Article < ApplicationRecord
./app/models/comment.rb:class Comment < ApplicationRecord
./config/application.rb:  class Application < Rails::Application
./config/environments/development.rb:  config.cache_classes = false
./config/environments/production.rb:  config.cache_classes = true
./config/environments/test.rb:  config.cache_classes = true
./db/migrate/20180320140857_create_articles.rb:class CreateArticles < ActiveRecord::Migration[5.1]
./db/migrate/20180320140956_create_comments.rb:class CreateComments < ActiveRecord::Migration[5.1]
./test/application_system_test_case.rb:class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
./test/controllers/articles_controller_test.rb:class ArticlesControllerTest < ActionDispatch::IntegrationTest
./test/controllers/comments_controller_test.rb:class CommentsControllerTest < ActionDispatch::IntegrationTest
./test/controllers/welcome_controller_test.rb:class WelcomeControllerTest < ActionDispatch::IntegrationTest
./test/models/article_test.rb:class ArticleTest < ActiveSupport::TestCase
./test/models/comment_test.rb:class CommentTest < ActiveSupport::TestCase
./test/test_helper.rb:class ActiveSupport::TestCase

これでディレクトリ・ファイル構成がざっと把握できた。

tags: ruby

Posted by NI-Lab. (@nilab)