Bulletin 모델의 생성
때로는 글의 성격에 따라 별도로 관리할 필요가 있다. 게시판
의 개념을 도입하면 글을 게시판별로 묶을 수 있다. 이를 위해서 Bulletin
이란 모델을 작성하기로 한다.
$ bin/rails g scaffold Bulletin title description:text
Running via Spring preloader in process 9842
invoke active_record
create db/migrate/20161213103856_create_bulletins.rb
create app/models/bulletin.rb
invoke test_unit
create test/models/bulletin_test.rb
create test/fixtures/bulletins.yml
invoke resource_route
route resources :bulletins
invoke scaffold_controller
create app/controllers/bulletins_controller.rb
invoke erb
create app/views/bulletins
create app/views/bulletins/index.html.erb
create app/views/bulletins/edit.html.erb
create app/views/bulletins/show.html.erb
create app/views/bulletins/new.html.erb
create app/views/bulletins/_form.html.erb
invoke test_unit
create test/controllers/bulletins_controller_test.rb
invoke helper
create app/helpers/bulletins_helper.rb
invoke test_unit
invoke jbuilder
create app/views/bulletins/index.json.jbuilder
create app/views/bulletins/show.json.jbuilder
create app/views/bulletins/_bulletin.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/bulletins.coffee
invoke scss
create app/assets/stylesheets/bulletins.scss
invoke scss
identical app/assets/stylesheets/scaffolds.scss
DB 마이그레이션 후 브라우저에서 확인해 보자.
$ bin/rails db:migrate
Running via Spring preloader in process 10428
== 20161213103856 CreateBulletins: migrating ==================================
-- create_table(:bulletins)
-> 0.0372s
== 20161213103856 CreateBulletins: migrated (0.0373s) =========================
이전의 posts
뷰 페이지들과 같이 아래의 뷰 파일들을 bootstrap
클래스로 스타일을 수정한 후 브라우저로 확인한다.
$ open http://localhost:3000/bulletins
index 액션과 뷰 템플릿 파일
app/controllers/bulletins_controller.rb
:
def index
@bulletins = Bulletin.all
end
app/views/bulletins/index.html.erb
:
<h2>Bulletins</h2>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Data actions</th>
</tr>
</thead>
<tbody>
<% @bulletins.each do |bulletin| %>
<tr>
<td><%= bulletin.title %></td>
<td><%= bulletin.description %></td>
<td>
<%= link_to 'Show', bulletin, class: 'btn btn-default' %>
<%= link_to 'Edit', edit_bulletin_path(bulletin), class: 'btn btn-default' %>
<%= link_to 'Destroy', bulletin, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default' %>
</td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Bulletin', new_bulletin_path, class: 'btn btn-default' %>
테스트용 데이터를 추가하면 아래와 같이 보인다.
show 액션과 뷰 템플릿 파일
show
액션 뷰 템플릿에서는 Title과 Description을 테이블 형식으로 표시하고 Created at이라는 항목을 추가하여 생성한 시각을 보여준다.
def show
end
<h2>Preview Bulletin</h2>
<table class='table table-bordered'>
<tr>
<th>Title</th>
<td><%= @bulletin.title %></td>
</tr>
<tr>
<th>Description</th>
<td><%= @bulletin.description %></td>
</tr>
<tr>
<th>Created at</th>
<td><%= @bulletin.created_at %></td>
</tr>
</table>
<%= link_to 'Edit', edit_bulletin_path(@bulletin), class: 'btn btn-default' %>
<%= link_to 'Back', bulletins_path, class: 'btn btn-default' %>
모든 뷰 템플릿을 수정해서 브라우저로 확인한 결과, 게시판을 생성한 시각이 UTC 타임존
으로 표시된다.
form 파셜 템플릿 파일
<%= simple_form_for(@bulletin) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :description, input_html: { rows: 5 } %>
</div>
<div class="form-actions">
<%= f.button :submit %>
<%= link_to 'Show', @bulletin, class: 'btn btn-default' if @bulletin.persisted? %>
<%= link_to 'List', bulletins_path, class: 'btn btn-default' %>
</div>
<% end %>
new 액션과 뷰 템플릿 파일
app/controllers/bulletins_controller.rb
:
def new
@bulletin = Bulletin.new
end
app/views/bulletins/new.html.erb
:
<h2>New bulletin</h2>
<%= render 'form' %>
edit 액션과 뷰 템플릿 파일
app/controllers/bulletins_controller.rb
:
def edit
end
app/views/bulletins/edit.html.erb
:
<h2>Editing bulletin</h2>
<%= render 'form' %>
타임존(Timezone)
위의 show
뷰 템플릿 화면캡쳐에서 Created at
(생성일) 값을 보면 2016-12-13 10:43:57 UTC
와 같다. 레일스의 디폴트 타임존 변경은 config/application.rb
파일에서 할 수 있다.
... 중략~
config.time_zone = 'Seoul'
... 중략~
레일스의 디폴트 타임존은 UTC
이고, 로컬 타임존 목록을 보기 위해서는 터미널에서 아래와 같이 명령을 실행한다.
$ bin/rails time:zones:all
* UTC -11:00 *
American Samoa
International Date Line West
Midway Island
Samoa
* UTC -10:00 *
Hawaii
* UTC -09:00 *
Alaska
* UTC -08:00 *
Pacific Time (US & Canada)
Tijuana
* UTC -07:00 *
Arizona
Chihuahua
Mazatlan
Mountain Time (US & Canada)
* UTC -06:00 *
Central America
Central Time (US & Canada)
Guadalajara
Mexico City
Monterrey
Saskatchewan
* UTC -05:00 *
Bogota
Eastern Time (US & Canada)
Indiana (East)
Lima
Quito
* UTC -04:00 *
Atlantic Time (Canada)
Caracas
Georgetown
La Paz
Santiago
* UTC -03:30 *
Newfoundland
* UTC -03:00 *
Brasilia
Buenos Aires
Greenland
Montevideo
* UTC -02:00 *
Mid-Atlantic
* UTC -01:00 *
Azores
Cape Verde Is.
* UTC +00:00 *
Casablanca
Dublin
Edinburgh
Lisbon
London
Monrovia
UTC
* UTC +01:00 *
Amsterdam
Belgrade
Berlin
Bern
Bratislava
Brussels
Budapest
Copenhagen
Ljubljana
Madrid
Paris
Prague
Rome
Sarajevo
Skopje
Stockholm
Vienna
Warsaw
West Central Africa
Zagreb
Zurich
* UTC +02:00 *
Athens
Bucharest
Cairo
Harare
Helsinki
Jerusalem
Kaliningrad
Kyiv
Pretoria
Riga
Sofia
Tallinn
Vilnius
* UTC +03:00 *
Baghdad
Istanbul
Kuwait
Minsk
Moscow
Nairobi
Riyadh
St. Petersburg
Volgograd
* UTC +03:30 *
Tehran
* UTC +04:00 *
Abu Dhabi
Baku
Muscat
Samara
Tbilisi
Yerevan
* UTC +04:30 *
Kabul
* UTC +05:00 *
Ekaterinburg
Islamabad
Karachi
Tashkent
* UTC +05:30 *
Chennai
Kolkata
Mumbai
New Delhi
Sri Jayawardenepura
* UTC +05:45 *
Kathmandu
* UTC +06:00 *
Almaty
Astana
Dhaka
Urumqi
* UTC +06:30 *
Rangoon
* UTC +07:00 *
Bangkok
Hanoi
Jakarta
Krasnoyarsk
Novosibirsk
* UTC +08:00 *
Beijing
Chongqing
Hong Kong
Irkutsk
Kuala Lumpur
Perth
Singapore
Taipei
Ulaanbaatar
* UTC +09:00 *
Osaka
Sapporo
Seoul
Tokyo
Yakutsk
* UTC +09:30 *
Adelaide
Darwin
* UTC +10:00 *
Brisbane
Canberra
Guam
Hobart
Melbourne
Port Moresby
Sydney
Vladivostok
* UTC +11:00 *
Magadan
New Caledonia
Solomon Is.
Srednekolymsk
* UTC +12:00 *
Auckland
Fiji
Kamchatka
Marshall Is.
Wellington
* UTC +12:45 *
Chatham Is.
* UTC +13:00 *
Nuku'alofa
Tokelau Is.
$ bin/rails time:zones:local
* UTC +09:00 *
Osaka
Sapporo
Seoul
Tokyo
Yakutsk
타임존을 Seoul
로 설정하기 위해서는 아래와 같이 값을 변경하고 로컬 웹서버 다시 시작한다. (config/application.rb)
...
module Rcafe2
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.time_zone = 'Seoul'
end
end
이제 Created at
값이 '2016-12-13 19:43:57 +0900' 와 같이 변경된 타임존에 맞게 나타나는 것을 볼 수 있다.
노트
이와 같이 타임존을 변경하여 시간을 해당 타임존에 맞게 표시할 수 있지만, 데이터베이스는 값을 항상 UTC 타임존으로 저장한다는 사실을 주목하자. DB로부터 UTC
로 저장된 시간을 불러와 표시할 때는 레일스가 config/application.rb 에 저장된 time_zone 값에 맞게 자동으로 변경한 후에 표시한다. 그러나, DB에 저장할 때도 로컬 타임존에 맞게 저장하려면 config.time_zone = 'Seoul'
아래에 config.active_record.default_timezone = :local
와 같이 추가해 주면 되지만, 궂이 이렇게 할 필요는 없다.