프로그램

RoR을 시작하는 코더의 기록 01

mulderu 2017. 5. 21. 11:14

오랜만에 IT 블로그입니다... 


ROR 말은 많이 들어 봤지만... 이제야 접근해 봅니다. 

많은 RoR자료가 인터넷에 많습니다... 

Java,.NET,NODE 등 Web Application의 구조를 대충 안다면 ... 바로 GO


1) 설치




2) Rails command line Utility 리뷰 (REF : http://guides.rubyonrails.org/command_line.html)

RoR에 접근한다면 가장 먼저 알아야 할 부분입니다.

 

rails new

새로운 Rails 어플리케이션을 생성하한다.

rails new commandsapp


rails server

rails server 명령어는 Puma 라는 웹서를 실행시킨다. (Puma는  Rails에 번들되어있다.)

bin/rails server

bundle exec rails server

bin/rails server -e production -p 4000


rails generate

템플릿화된 모든것들을 생성해준다.


bin/rails generate 


bin/rails generate controller NAME [action action] [options]

Example:

    `rails generate controller CreditCards open debit credit close`


아래는 실제 테슽해본결과임

Example:

mulder@~/Project/mulder/railsapp/test_app$ bin/rails generate controller Greetings hello

Running via Spring preloader in process 56538

      create  app/controllers/greetings_controller.rb

       route  get 'greetings/hello'

      invoke  erb

      create    app/views/greetings

      create    app/views/greetings/hello.html.erb

      invoke  test_unit

      create    test/controllers/greetings_controller_test.rb

      invoke  helper

      create    app/helpers/greetings_helper.rb

      invoke    test_unit

      invoke  assets

      invoke    coffee

      create      app/assets/javascripts/greetings.coffee

      invoke    scss

      create      app/assets/stylesheets/greetings.scss

mulder@~/Project/mulder/railsapp/test_app$ 


mulder@~/Project/mulder/railsapp/test_app$ cat app/controllers/greetings_controller.rb

class GreetingsController < ApplicationController

  def hello

  end

end

mulder@~/Project/mulder/railsapp/test_app$


조금 수정함


mulder@~/Project/mulder/railsapp/test_app$ cat app/controllers/greetings_controller.rb

class GreetingsController < ApplicationController

  def hello

    @message = "Hello, how are you today?"

  end

end

mulder@~/Project/mulder/railsapp/test_app$ cat app/views/greetings/hello.html.erb

<h1>Greetings#hello</h1>

<p>Find me in app/views/greetings/hello.html.erb</p>

<p><%= @message %></p>


open : http://localhost:3000/greetings/hello



bin/rails generate model

rails generate model NAME [field[:type][:index] field[:type]


model을 이용하지 말고 scaffold 를 이용하자, model의 모든기능을 제공해 준다.

scaffold를 이용하면  model migration, controller , view 등 MVC의 모든 기능을 한칼에 제공해준다.


example)

mulder@~/Project/mulder/railsapp/test_app$ bin/rails generate scaffold HighScore game:string score:integer

Running via Spring preloader in process 56606

      invoke  active_record

      create    db/migrate/20170521014246_create_high_scores.rb

      create    app/models/high_score.rb

      invoke    test_unit

      create      test/models/high_score_test.rb

      create      test/fixtures/high_scores.yml

      invoke  resource_route

       route    resources :high_scores

      invoke  scaffold_controller

      create    app/controllers/high_scores_controller.rb

      invoke    erb

      create      app/views/high_scores

      create      app/views/high_scores/index.html.erb

      create      app/views/high_scores/edit.html.erb

      create      app/views/high_scores/show.html.erb

      create      app/views/high_scores/new.html.erb

      create      app/views/high_scores/_form.html.erb

      invoke    test_unit

      create      test/controllers/high_scores_controller_test.rb

      invoke    helper

      create      app/helpers/high_scores_helper.rb

      invoke      test_unit

      invoke    jbuilder

      create      app/views/high_scores/index.json.jbuilder

      create      app/views/high_scores/show.json.jbuilder

      create      app/views/high_scores/_high_score.json.jbuilder

      invoke  test_unit

      create    test/system/high_scores_test.rb

      invoke  assets

      invoke    coffee

      create      app/assets/javascripts/high_scores.coffee

      invoke    scss

      create      app/assets/stylesheets/high_scores.scss

      invoke  scss

      create    app/assets/stylesheets/scaffolds.scss

mulder@~/Project/mulder/railsapp/test_app$ 


RUN :

mulder@~/Project/mulder/railsapp/test_app$ bin/rails db:migrate


== 20170521014246 CreateHighScores: migrating =================================

-- create_table(:high_scores)

   -> 0.0007s

== 20170521014246 CreateHighScores: migrated (0.0008s) ========================


mulder@~/Project/mulder/railsapp/test_app$ 

mulder@~/Project/mulder/railsapp/test_app$ bin/rails server

open: http://localhost:3000/high_scores



About: check application informations


mulder@~/Project/mulder/railsapp/test_app$ bin/rails about

About your application's environment

Rails version             5.1.1

Ruby version              2.4.0-p0 (x86_64-darwin16)

RubyGems version          2.6.12

Rack version              2.0.3

JavaScript Runtime        Node.js (V8)

Middleware                Rack::Sendfile, ActionDispatch::Static, ActionDispatch::Executor, ActiveSupport::Cache::Strategy::LocalCache::Middleware, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, ActionDispatch::RemoteIp, Sprockets::Rails::QuietAssets, Rails::Rack::Logger, ActionDispatch::ShowExceptions, WebConsole::Middleware, ActionDispatch::DebugExceptions, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, Rack::Head, Rack::ConditionalGet, Rack::ETag

Application root          /Users/mulder/Project/mulder/railsapp/test_app

Environment               development

Database adapter          sqlite3

Database schema version   20170521014246

mulder@~/Project/mulder/railsapp/test_app$ 

mulder@~/Project/mulder/railsapp/test_app$ bin/rails db:version

Current version: 20170521014246

mulder@~/Project/mulder/railsapp/test_app$ 


WEB Route

mulder@~/Project/mulder/railsapp/test_app$ rails routes

         Prefix Verb   URI Pattern                     Controller#Action

    high_scores GET    /high_scores(.:format)          high_scores#index

                POST   /high_scores(.:format)          high_scores#create

 new_high_score GET    /high_scores/new(.:format)      high_scores#new

edit_high_score GET    /high_scores/:id/edit(.:format) high_scores#edit

     high_score GET    /high_scores/:id(.:format)      high_scores#show

                PATCH  /high_scores/:id(.:format)      high_scores#update

                PUT    /high_scores/:id(.:format)      high_scores#update

                DELETE /high_scores/:id(.:format)      high_scores#destroy

greetings_hello GET    /greetings/hello(.:format)      greetings#hello

mulder@~/Project/mulder/railsapp/test_app$ 


mulder@~/Project/mulder/railsapp/test_app$ rails stats

+----------------------+--------+--------+---------+---------+-----+-------+

| Name                 |  Lines |    LOC | Classes | Methods | M/C | LOC/M |

+----------------------+--------+--------+---------+---------+-----+-------+

| Controllers          |     82 |     58 |       3 |      10 |   3 |     3 |

| Helpers              |      6 |      6 |       0 |       0 |   0 |     0 |

| Jobs                 |      2 |      2 |       1 |       0 |   0 |     0 |

| Models               |      5 |      5 |       2 |       0 |   0 |     0 |

| Mailers              |      4 |      4 |       1 |       0 |   0 |     0 |

| Channels             |      8 |      8 |       2 |       0 |   0 |     0 |

| JavaScripts          |     34 |      4 |       0 |       1 |   0 |     2 |

| Libraries            |      0 |      0 |       0 |       0 |   0 |     0 |

| Controller tests     |     57 |     45 |       2 |       8 |   4 |     3 |

| Helper tests         |      0 |      0 |       0 |       0 |   0 |     0 |

| Model tests          |      7 |      3 |       1 |       0 |   0 |     0 |

| Mailer tests         |      0 |      0 |       0 |       0 |   0 |     0 |

| Integration tests    |      0 |      0 |       0 |       0 |   0 |     0 |

| System tests         |      9 |      3 |       1 |       0 |   0 |     0 |

+----------------------+--------+--------+---------+---------+-----+-------+

| Total                |    214 |    138 |      13 |      19 |   1 |     5 |

+----------------------+--------+--------+---------+---------+-----+-------+

  Code LOC: 87     Test LOC: 51     Code to Test Ratio: 1:0.6


mulder@~/Project/mulder/railsapp/test_app$ 


DATABase 세팅 :

mulder@~/Project/mulder/railsapp/test_app$ cat config/database.yml

# SQLite version 3.x

#   gem install sqlite3

#

#   Ensure the SQLite 3 gem is defined in your Gemfile

#   gem 'sqlite3'

#

default: &default

  adapter: sqlite3

  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

  timeout: 5000


development:

  <<: *default

  database: db/development.sqlite3


# Warning: The database defined as "test" will be erased and

# re-generated from your development 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

mulder@~/Project/mulder/railsapp/test_app$