N1. NoAR 만들기 - post 컨트롤러 생성

2020. 7. 5. 16:35Rails 5 on aws c9

1. NoAR 만들기 - post 컨트롤러 생성

이제 본격적으로 noar를 만들어보자

ubuntu:~/environment $ rails new noar
      create  
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/config/manifest.js

            ...

      create  vendor/assets/stylesheets/.keep
      remove  config/initializers/cors.rb
         run  bundle install
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 13.0.1

...

For more info see:
https://github.com/svenfuchs/i18n/releases/tag/v1.1.0

         run  bundle exec spring binstub --all
* bin/rake: Spring inserted
* bin/rails: Spring inserted

그리고 이 프로젝트도 git으로 관리해보자.

**ubuntu:~/environment/noar $ git init**
Initialized empty Git repository in /home/ubuntu/environment/noar/.git/

**ubuntu:~/environment/noar (master) $ git status**
On branch master
No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        Gemfile
        Gemfile.lock
        README.md
        Rakefile
        app/
        bin/
        config.ru
        config/
        db/
        lib/
        log/
        public/
        test/
        tmp/
        vendor/

nothing added to commit but untracked files present (use "git add" to track)
ubuntu:~/environment/noar (master) $ git add .
ubuntu:~/environment/noar (master) $ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

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

**ubuntu:~/environment/noar (master) $ git commit -m "init"**
[master (root-commit) 17fb01f] init
 75 files changed, 1143 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock
 create mode 100644 README.md
 create mode 100644 Rakefile
 create mode 100644 app/assets/config/manifest.js
 create mode 100644 app/assets/images/.keep
 create mode 100644 app/assets/javascripts/application.js
 create mode 100644 app/assets/javascripts/cable.js
 create mode 100644 app/assets/javascripts/channels/.keep
 create mode 100644 app/assets/stylesheets/application.css
 create mode 100644 app/channels/application_cable/channel.rb
 create mode 100644 app/channels/application_cable/connection.rb
 create mode 100644 app/controllers/application_controller.rb
 create mode 100644 app/controllers/concerns/.keep
 create mode 100644 app/helpers/application_helper.rb
 create mode 100644 app/jobs/application_job.rb
 create mode 100644 app/mailers/application_mailer.rb
 create mode 100644 app/models/application_record.rb
 create mode 100644 app/models/concerns/.keep
 create mode 100644 app/views/layouts/application.html.erb
 create mode 100644 app/views/layouts/mailer.html.erb
 create mode 100644 app/views/layouts/mailer.text.erb
 create mode 100755 bin/bundle
 create mode 100755 bin/rails
 create mode 100755 bin/rake
 create mode 100755 bin/setup
 create mode 100755 bin/spring
 create mode 100755 bin/update
 create mode 100644 config.ru
 create mode 100644 config/application.rb
 create mode 100644 config/boot.rb
 create mode 100644 config/cable.yml
 create mode 100644 config/database.yml
 create mode 100644 config/environment.rb
 create mode 100644 config/environments/development.rb
 create mode 100644 config/environments/production.rb
 create mode 100644 config/environments/test.rb
 create mode 100644 config/initializers/application_controller_renderer.rb
 create mode 100644 config/initializers/assets.rb
 create mode 100644 config/initializers/backtrace_silencers.rb
 create mode 100644 config/initializers/cookies_serializer.rb
 create mode 100644 config/initializers/filter_parameter_logging.rb
 create mode 100644 config/initializers/inflections.rb
 create mode 100644 config/initializers/mime_types.rb
 create mode 100644 config/initializers/new_framework_defaults.rb
 create mode 100644 config/initializers/session_store.rb
 create mode 100644 config/initializers/wrap_parameters.rb
 create mode 100644 config/locales/en.yml
 create mode 100644 config/puma.rb
 create mode 100644 config/routes.rb
 create mode 100644 config/secrets.yml
 create mode 100644 config/spring.rb
 create mode 100644 db/seeds.rb
 create mode 100644 lib/assets/.keep
 create mode 100644 lib/tasks/.keep
 create mode 100644 log/.keep
 create mode 100644 public/404.html
 create mode 100644 public/422.html
 create mode 100644 public/500.html
 create mode 100644 public/apple-touch-icon-precomposed.png
 create mode 100644 public/apple-touch-icon.png
 create mode 100644 public/favicon.ico
 create mode 100644 public/robots.txt
 create mode 100644 test/controllers/.keep
 create mode 100644 test/fixtures/.keep
 create mode 100644 test/fixtures/files/.keep
 create mode 100644 test/helpers/.keep
 create mode 100644 test/integration/.keep
 create mode 100644 test/mailers/.keep
 create mode 100644 test/models/.keep
 create mode 100644 test/test_helper.rb
 create mode 100644 tmp/.keep
 create mode 100644 vendor/assets/javascripts/.keep
 create mode 100644 vendor/assets/stylesheets/.keep

**ubuntu:~/environment/noar (master) $ git log**
commit 17fb01fa5eeb3efac3dcedbfae0affc1d8ac78f6 (HEAD -> master)
Author: comper <comp-er@daum.net>
Date:   Sat Jun 13 10:49:10 2020 +0000

    init

그럼 이제 올릴 글(post)을 관리해줄 컨트롤러를 만들어보자.

(혹시 안된다면 Gemfile 의 gem 'sqlite3' 를 gem 'sqlite3', '~> 1.3.0'로 수정하고 bundle를 한번 실행시켜주자)

ubuntu:~/environment/noar (master) $ rails g controller Posts index
Running via Spring preloader in process 22751
      create  app/controllers/posts_controller.rb
       route  get 'posts/index'
      invoke  erb
      create    app/views/posts
      create    app/views/posts/index.html.erb
      invoke  test_unit
      create    test/controllers/posts_controller_test.rb
      invoke  helper
      create    app/helpers/posts_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/posts.coffee
      invoke    scss
      create      app/assets/stylesheets/posts.scss

그 후 routes.rb파일 수정

Rails.application.routes.draw do
  root 'posts#index'
    #상세보기에 해당하는 show함수 빼고 다 라우트 규칙 생성
  resources :posts, except: [:show]

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

그리고 서버 실행

ubuntu:~/environment/noar (master) $ rails s
=> Booting Puma
=> Rails 5.0.7.2 application starting in development on http://localhost:8080
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.3-p62), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:8080
Use Ctrl-C to stop