Ruby on Rails Hello World 프로젝트에서
개시하다
일 때문에 루비온 레일스를 만났어요.
이번에는 기본 웹 응용 프로그램을 만들기 전의 절차를 배웁니다.
(엔지니어링, 모델/뷰/컨트롤러 제작, DB 데이터 출력까지)
사용 환경
나는 아래의 환경에서 실시하고 싶다.> ruby -v
ruby 2.5.3p105 (2018-10-18 revision 65156) [x64-mingw32]
> bundle -v
Bundler version 1.17.1
> rails -v
Rails 5.2.1
MySQL도 있어요. (해당되는 버전으로!)
절차.
bundle의 준비
프로젝트 폴더 init를 먼저 만듭니다.> mkdir HelloWorld
> cd HelloWorld
> bundle init
> ./Gemfile
Gemfile 변경 사항은 다음과 같습니다.
> bundle install --path vendor/bundle
상기 "-path vender/bundle"옵션을 추가하여 설치하면 프로젝트 폴더 (이 때 Hello World 아래) 에gem를 추가할 수 있습니다.
프로젝트 단위로 관리할 수 있다는 것이다.
rails 프로젝트 만들기
버블의 준비가 끝났기 때문에 이곳에서 rails 프로젝트를 제작합니다.// -B でBundle installをスキップできる
// -d で使用するDBを指定できる
// --skip-turbolinks でturbolinksをオフにする(今回はスキップ)
bundle exec rails new . -B -d mysql --skip-turbolinks
Overwrite C:/ruby_workspace/HelloWorld/Gemfile? (enter "h" for help) [Ynaqdhm]
그래서 "네"로 덮어씁니다.
현재 rails 프로젝트의 제작이 완료되었습니다.
동작 확인(실패)
실제 가동 여부를 확인하다.> bundle exec rails server
Could not find gem 'mysql2 (< 0.6.0, >= 0.4.4) x64-mingw32' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
oops... Gemfile 변경 후 bundle install 입니다.bundle install --path vendor/bundle
bundle exec rails server
DB가 설정되어 있지 않습니다.
DB 설정
DB 연결 대상 설정
config/database.ymldefault: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password ←ここだけ変更
host: localhost
development:
<<: *default
database: HelloWorld_test_development
test:
<<: *default
database: HelloWorld_test_test
production:
<<: *default
database: HelloWorld_test_production
username: HelloWorld_test
password: <%= ENV['HELLOWORLD_TEST_DATABASE_PASSWORD'] %>
DB 생성
> bundle exec rake db:create
Created database 'HelloWorld_development'
Created database 'HelloWorld_test'
동작 확인(이번에야말로)
> bundle exec rails server
초기 화면 순조롭게 작동 완료!
그래서 MVC를 사용한 헬로월드를 하고 싶다.
MVC의 모형, 표 제작
나는 아래의 환경에서 실시하고 싶다.
> ruby -v
ruby 2.5.3p105 (2018-10-18 revision 65156) [x64-mingw32]
> bundle -v
Bundler version 1.17.1
> rails -v
Rails 5.2.1
MySQL도 있어요. (해당되는 버전으로!)
절차.
bundle의 준비
프로젝트 폴더 init를 먼저 만듭니다.> mkdir HelloWorld
> cd HelloWorld
> bundle init
> ./Gemfile
Gemfile 변경 사항은 다음과 같습니다.
> bundle install --path vendor/bundle
상기 "-path vender/bundle"옵션을 추가하여 설치하면 프로젝트 폴더 (이 때 Hello World 아래) 에gem를 추가할 수 있습니다.
프로젝트 단위로 관리할 수 있다는 것이다.
rails 프로젝트 만들기
버블의 준비가 끝났기 때문에 이곳에서 rails 프로젝트를 제작합니다.// -B でBundle installをスキップできる
// -d で使用するDBを指定できる
// --skip-turbolinks でturbolinksをオフにする(今回はスキップ)
bundle exec rails new . -B -d mysql --skip-turbolinks
Overwrite C:/ruby_workspace/HelloWorld/Gemfile? (enter "h" for help) [Ynaqdhm]
그래서 "네"로 덮어씁니다.
현재 rails 프로젝트의 제작이 완료되었습니다.
동작 확인(실패)
실제 가동 여부를 확인하다.> bundle exec rails server
Could not find gem 'mysql2 (< 0.6.0, >= 0.4.4) x64-mingw32' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
oops... Gemfile 변경 후 bundle install 입니다.bundle install --path vendor/bundle
bundle exec rails server
DB가 설정되어 있지 않습니다.
DB 설정
DB 연결 대상 설정
config/database.ymldefault: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password ←ここだけ変更
host: localhost
development:
<<: *default
database: HelloWorld_test_development
test:
<<: *default
database: HelloWorld_test_test
production:
<<: *default
database: HelloWorld_test_production
username: HelloWorld_test
password: <%= ENV['HELLOWORLD_TEST_DATABASE_PASSWORD'] %>
DB 생성
> bundle exec rake db:create
Created database 'HelloWorld_development'
Created database 'HelloWorld_test'
동작 확인(이번에야말로)
> bundle exec rails server
초기 화면 순조롭게 작동 완료!
그래서 MVC를 사용한 헬로월드를 하고 싶다.
MVC의 모형, 표 제작
> mkdir HelloWorld
> cd HelloWorld
> bundle init
> ./Gemfile
> bundle install --path vendor/bundle
// -B でBundle installをスキップできる
// -d で使用するDBを指定できる
// --skip-turbolinks でturbolinksをオフにする(今回はスキップ)
bundle exec rails new . -B -d mysql --skip-turbolinks
> bundle exec rails server
Could not find gem 'mysql2 (< 0.6.0, >= 0.4.4) x64-mingw32' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.
bundle install --path vendor/bundle
bundle exec rails server
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password ←ここだけ変更
host: localhost
development:
<<: *default
database: HelloWorld_test_development
test:
<<: *default
database: HelloWorld_test_test
production:
<<: *default
database: HelloWorld_test_production
username: HelloWorld_test
password: <%= ENV['HELLOWORLD_TEST_DATABASE_PASSWORD'] %>
> bundle exec rake db:create
Created database 'HelloWorld_development'
Created database 'HelloWorld_test'
> bundle exec rails server
> rails generate model hello_model
invoke active_record
create db/migrate/20181119015611_create_hello_models.rb
create app/models/hello_model.rb
invoke test_unit
create test/models/hello_model_test.rb
create test/fixtures/hello_models.yml
t.[型] :[テーブル名]
처럼 기재)db/migrate/20181119015611_create_hello_models.rb
class CreateHelloModels < ActiveRecord::Migration[5.2]
def change
create_table :hello_models do |t|
t.string :hello_text
t.timestamps
end
end
end
다음 명령을 사용하여 생성합니다.> bundle exec rake db:migrate
mysql> desc hello_models;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| hello_text | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
샘플 데이터 제작
db 디렉터리의 "seed.rb"편집
db/seed.rb
HelloModel.create([{ hello_text: 'Hello' }, { hello_text: 'World'}])
다음 명령을 사용하여 샘플 데이터 만들기> bundle exec rake db:seed
mysql> select * from hello_models;
+----+------------+---------------------+---------------------+
| id | hello_text | created_at | updated_at |
+----+------------+---------------------+---------------------+
| 1 | Hello | 2018-11-19 02:56:55 | 2018-11-19 02:56:55 |
| 2 | World | 2018-11-19 02:56:55 | 2018-11-19 02:56:55 |
+----+------------+---------------------+---------------------+
2 rows in set (0.00 sec)
MVC의 보기, 컨트롤러 생성
bundle exec rails generate controller hello_controller hello_view
<h1><%=@hello.hello_text %>!<%=@world.hello_text %>!!</h1>
<p>Find me in app/views/hello_controller/hello_view.html.erb</p>
<p>
foreach check ->
<% @hello_world.each do |text| %>
<%= text.hello_text %>
<% end %>
!!
</p>
실행 확인> bundle exec rails server (起動している場合は不要)
다 했어!
보태다
bundle exec rails destroy controller hello_controller hello_view
bundle exec rails destroy model hello_model
이름 규칙상 Hello만 좋은 것 같아요.(낙타)
참고 자료
새 Rails 프로젝트의 제작 단계 요약
https://qiita.com/yuitnnn/items/b45bba658d86eabdbb26
DB에 익숙해져라!Ruby on Rails의 Rake 명령으로 데이터베이스를 만드는 방법[초보자용]
https://techacademy.jp/magazine/7207
MySQL5.7에서 MySQL8까지.0으로 업그레이드하면 오류가 발생합니다.
https://qiita.com/yoshi1729/items/8a023d28c92dc5e62f86
Reference
이 문제에 관하여(Ruby on Rails Hello World 프로젝트에서), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/knoth071211/items/8627c33fcd926ff014a4
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(Ruby on Rails Hello World 프로젝트에서), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/knoth071211/items/8627c33fcd926ff014a4텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)