Rails

#### Ruby/Rails install
```
[root@ARSFW001 data]# export RBENV_ROOT=/usr/local/rbenv
[root@ARSFW001 data]# export PATH=${RBENV_ROOT}/bin:${PATH}
[root@ARSFW001 data]# git clone http://github.com/sstephenson/rbenv.git ${RBENV_ROOT}
[root@ARSFW001 data]# git clone http://github.com/sstephenson/ruby-build.git ${RBENV_ROOT}/plugins/ruby-build

[root@ARSFW001 data]# cat > /etc/profile.d/rbenv.sh << EOF
[root@ARSFW001 data]# export RBENV_ROOT="/usr/local/rbenv"
[root@ARSFW001 data]# export PATH="\${RBENV_ROOT}/bin:\${PATH}"
[root@ARSFW001 data]# eval "\$(rbenv init -)"
[root@ARSFW001 data]# EOF
[root@ARSFW001 data]# cat /etc/profile.d/rbenv.sh
[root@ARSFW001 data]# yum install -y openssl-devel readline-devel zlib-devel

[root@ARSFW001 data]# rbenv install [ruby version]
[root@ARSFW001 data]# rbenv global [ruby version]

[root@ARSFW001 data]# gem install rails
```


#### rails作成、controller作成
```
rails new design-requirements -d mysql
rails g controller requirements index show
```


```
[root@ARSFW001 data]# rails new design-requirements -d mysql
      create
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
         run  git init from "."
Initialized empty Git repository in /var/opt/data/design-requirements/.git/
      create  app
      create  app/assets/config/manifest.js
      create  app/assets/javascripts/application.js
      create  app/assets/javascripts/cable.js
      create  app/assets/stylesheets/application.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/jobs/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/layouts/mailer.text.erb
      create  app/assets/images/.keep
      create  app/assets/javascripts/channels
      create  app/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
      create  bin/yarn
      create  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/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/application_controller_renderer.rb
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/cors.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_framework_defaults_5_1.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/apple-touch-icon-precomposed.png
      create  public/apple-touch-icon.png
      create  public/favicon.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/mailers
      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/test_helper.rb
      create  test/system
      create  test/system/.keep
      create  test/application_system_test_case.rb
      create  tmp
      create  tmp/.keep
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor
      create  vendor/.keep
      create  package.json
      remove  config/initializers/cors.rb
      remove  config/initializers/new_framework_defaults_5_1.rb
         run  bundle install
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies......
Using rake 12.3.0
Using concurrent-ruby 1.0.5
Using i18n 0.9.1
Using minitest 5.10.3
Using thread_safe 0.3.6
Using tzinfo 1.2.4
Using activesupport 5.1.4
Using builder 3.2.3
Using erubi 1.7.0
Using mini_portile2 2.3.0
Using nokogiri 1.8.1
Using rails-dom-testing 2.0.3
Using crass 1.0.3
Using loofah 2.1.1
Using rails-html-sanitizer 1.0.3
Using actionview 5.1.4
Using rack 2.0.3
Using rack-test 0.7.0
Using actionpack 5.1.4
Using nio4r 2.1.0
Using websocket-extensions 0.1.3
Using websocket-driver 0.6.5
Using actioncable 5.1.4
Using globalid 0.4.1
Using activejob 5.1.4
Using mini_mime 1.0.0
Using mail 2.7.0
Using actionmailer 5.1.4
Using activemodel 5.1.4
Using arel 8.0.0
Using activerecord 5.1.4
Using public_suffix 3.0.1
Using addressable 2.5.2
Using bindex 0.5.0
Using bundler 1.16.0
Using byebug 9.1.0
Using xpath 2.1.0
Using capybara 2.16.0
Using ffi 1.9.18
Using childprocess 0.8.0
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using coffee-script 2.4.1
Using method_source 0.9.0
Using thor 0.20.0
Using railties 5.1.4
Using coffee-rails 4.2.2
Using multi_json 1.12.2
Using jbuilder 2.7.0
Using rb-fsevent 0.10.2
Using rb-inotify 0.9.10
Using ruby_dep 1.5.0
Using listen 3.1.5
Using mysql2 0.4.10
Using puma 3.10.0
Using sprockets 3.7.1
Using sprockets-rails 3.2.1
Using rails 5.1.4
Using rubyzip 1.2.1
Using sass-listen 4.0.0
Using sass 3.5.3
Using tilt 2.0.8
Using sass-rails 5.0.7
Using selenium-webdriver 3.7.0
Using spring 2.0.2
Using spring-watcher-listen 2.0.1
Using turbolinks-source 5.0.3
Using turbolinks 5.0.1
Using uglifier 3.2.0
Using web-console 3.5.1
Bundle complete! 16 Gemfile dependencies, 70 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted


[root@ARSFW001 data]# cd design-requirements/
[root@ARSFW001 design-requirements]# rails g controller requirements index show
Running via Spring preloader in process 9248
      create  app/controllers/requirements_controller.rb
       route  get 'requirements/show'
       route  get 'requirements/index'
      invoke  erb
      create    app/views/requirements
      create    app/views/requirements/index.html.erb
      create    app/views/requirements/show.html.erb
      invoke  test_unit
      create    test/controllers/requirements_controller_test.rb
      invoke  helper
      create    app/helpers/requirements_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/requirements.coffee
      invoke    scss
      create      app/assets/stylesheets/requirements.scss



```


(別ターミナル)
cd design-requirements
rails s

http://[Address]:3000/requirements/show でブラウザから接続する

![image.png](https://qiita-image-store.s3.amazonaws.com/0/215043/6daaffad-460e-900e-33be-0b0712b2b350.png)

### DBとの連携
#### DB接続のためのユーザ、パスワードの設定

vi config/database.yml


>default: &default
>  adapter: mysql2
>  encoding: utf8
>  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
>  username: [username]  << DBに接続するユーザ
>  password: [password]  << DBに接続するパスワード
>  socket: /var/lib/mysql/mysql.sock
> .....
```
rails用のユーザを用いる場合はあらかじめMySQL上でユーザを作成しておく。
```

#### DBの作成
```
[root@ARSFW001 design-requirements]# rails db:create
Created database 'design-requirements_development'
Created database 'design-requirements_test'
```

#### DBのテーブル作成、反映

```
rails generate scaffold requirement hostname:string cpu:integer memory:integer

[root@ARSFW001 design-requirements]# rails generate scaffold requirement hostname:string cpu:integer memory:integer
Running via Spring preloader in process 3399
      invoke  active_record
      create    db/migrate/20171119103911_create_requirements.rb
      create    app/models/requirement.rb
      invoke    test_unit
      create      test/models/requirement_test.rb
      create      test/fixtures/requirements.yml
      invoke  resource_route
       route    resources :requirements
      invoke  scaffold_controller
   identical    app/controllers/requirements_controller.rb
      invoke    erb
       exist      app/views/requirements
   identical      app/views/requirements/index.html.erb
   identical      app/views/requirements/edit.html.erb
   identical      app/views/requirements/show.html.erb
   identical      app/views/requirements/new.html.erb
   identical      app/views/requirements/_form.html.erb
      invoke    test_unit
   identical      test/controllers/requirements_controller_test.rb
      invoke    helper
   identical      app/helpers/requirements_helper.rb
      invoke      test_unit
      invoke    jbuilder
   identical      app/views/requirements/index.json.jbuilder
   identical      app/views/requirements/show.json.jbuilder
   identical      app/views/requirements/_requirement.json.jbuilder
      invoke  test_unit
   identical    test/system/requirements_test.rb
      invoke  assets
      invoke    coffee
   identical      app/assets/javascripts/requirements.coffee
      invoke    scss
   identical      app/assets/stylesheets/requirements.scss
      invoke  scss
   identical    app/assets/stylesheets/scaffolds.scss
```


※以下のような、モデル名によっては複数形を誤認識することがあるので、削除してやりなおす 削除の際も複数形の誤認識がある場合は "--force-plural" を付与して実行しなおす

```
rails generate scaffold requirements hostname:string cpu:integer memory:integer

[root@ARSFW001 design-requirements]# rails generate scaffold requirements hostname:string cpu:integer memory:integer
Running via Spring preloader in process 17958
[WARNING] The model name 'requirements' was recognized as a plural, using the singular 'requirement' instead. Override with --force-plural or setup custom inflection rules for this noun before running the generator.
      invoke  active_record
      create    db/migrate/20171119103216_create_requirements.rb
      create    app/models/requirement.rb
      invoke    test_unit
      create      test/models/requirement_test.rb
      create      test/fixtures/requirements.yml
      invoke  resource_route
       route    resources :requirements
      invoke  scaffold_controller
    conflict    app/controllers/requirements_controller.rb
  Overwrite /var/opt/data/design-requirements/app/controllers/requirements_controller.rb? (enter "h" for help) [Ynaqdh] Y
       force    app/controllers/requirements_controller.rb
      invoke    erb
       exist      app/views/requirements
    conflict      app/views/requirements/index.html.erb
    Overwrite /var/opt/data/design-requirements/app/views/requirements/index.html.erb? (enter "h" for help) [Ynaqdh] Y
       force      app/views/requirements/index.html.erb
      create      app/views/requirements/edit.html.erb
    conflict      app/views/requirements/show.html.erb
    Overwrite /var/opt/data/design-requirements/app/views/requirements/show.html.erb? (enter "h" for help) [Ynaqdh] Y
       force      app/views/requirements/show.html.erb
      create      app/views/requirements/new.html.erb
      create      app/views/requirements/_form.html.erb
      invoke    test_unit
    conflict      test/controllers/requirements_controller_test.rb
    Overwrite /var/opt/data/design-requirements/test/controllers/requirements_controller_test.rb? (enter "h" for help) [Ynaqdh] Y
       force      test/controllers/requirements_controller_test.rb
      invoke    helper
   identical      app/helpers/requirements_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/requirements/index.json.jbuilder
      create      app/views/requirements/show.json.jbuilder
      create      app/views/requirements/_requirement.json.jbuilder
      invoke  test_unit
      create    test/system/requirements_test.rb
      invoke  assets
      invoke    coffee
   identical      app/assets/javascripts/requirements.coffee
      invoke    scss
   identical      app/assets/stylesheets/requirements.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.scss

[root@ARSFW001 design-requirements]# rails db:migrate

[root@ARSFW001 design-requirements]# rails destroy model requirements
Running via Spring preloader in process 19184
[WARNING] The model name 'requirements' was recognized as a plural, using the singular 'requirement' instead. Override with --force-plural or setup custom inflection rules for this noun before running the generator.
      invoke  active_record
      remove    db/migrate/20171119103216_create_requirements.rb
      remove    app/models/requirement.rb
      invoke    test_unit
      remove      test/models/requirement_test.rb
      remove      test/fixtures/requirements.yml


[root@ARSFW001 design-requirements]# rails destroy model input_requirements
Running via Spring preloader in process 4393
[WARNING] The model name 'input_requirements' was recognized as a plural, using the singular 'input_requirement' instead. Override with --force-plural or setup custom inflection rules for this noun before running the generator.
      invoke  active_record
      remove    db/migrate/20171119111507_create_input_requirements.rb
      remove    app/models/input_requirement.rb
      invoke    test_unit
      remove      test/models/input_requirement_test.rb
      remove      test/fixtures/input_requirements.yml

[root@ARSFW001 design-requirements]# rails destroy model input_requirements --force-plural
Running via Spring preloader in process 12467
      invoke  active_record
      remove    db/migrate/20171119111519_create_input_requirements.rb
      remove    app/models/input_requirements.rb
      invoke    test_unit
      remove      test/models/input_requirements_test.rb
      remove      test/fixtures/input_requirements.yml
```

#### railsと連携
```
[root@ARSFW001 design-requirements]# rails db:migrate
== 20171119103911 CreateRequirements: migrating ===============================
-- create_table(:requirements)
   -> 0.0434s
== 20171119103911 CreateRequirements: migrated (0.0435s) ======================
```

#### requirement を入力するモデルの作成　（いらないかも）
```
rails generate scaffold input_requirement hostname:string cpu:integer memory:integer
```

#### requirement を入力する画面の作成
以下のように```app/views/requirements/new.html.erb```を編集する。

```
[root@ARSFW001 design-requirements]# vi app/views/requirements/new.html.erb
```

>
<%= form_for Requirement.new do |f| %>
  <%= f.label :hostname %>
  <%= f.text_field :hostname %>
  <%= f.label :cpu %>
  <%= f.text_area :cpu %>
  <%= f.label :memory %>
  <%= f.text_area :memory %>
  <%= f.submit %>
<% end %>


### Bootstrap の導入
design-requirements/Gemfile に以下を追加。

```
gem 'rails', '~> 5.1.4'
gem 'bootstrap-sass', '3.3.7' <<< 追加
```

bundle installで反映

```
bundle install
```

```
touch app/assets/stylesheets/custom.scss
```

<BR><BR>
***
### 参考資料
「小学生でもわかるRuby on Rails入門」
https://openbook4.me/projects/92/sections/488

「Ruby on Rails チュートリアル」
https://railstutorial.jp/chapters/toy_app?version=5.1#cha-a_toy_app

「Ruby on Rails チュートリアル(Rails v5.1.0)でBootstrapが適用できない問題」
https://qiita.com/motty93/items/6fd63a6b01bd6bb142a8

「Rails の Model 作成時に単数形・複数形の変換がおかしい場合の対処法」
https://qiita.com/Salinger/items/c48f999d4d7211f16fce

「railsで使わなかった物の削除で迷う」
https://qiita.com/seisonshi/items/dbc7e8ef08e4ebe9a1f1

「Ruby on Railsでテーブル削除」　＜＜参考にならず
https://qiita.com/Kuragasaki/items/7d98cf29341611fb189a

「Railsでモデルとテーブルを削除」
http://hbnist76.blog.fc2.com/blog-entry-270.html