0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails初心者〜構築からMVC開発〜

Last updated at Posted at 2016-08-30

Ruby on Railsの環境構築

  1. gemとrails

1.1. Rubyのバージョンを確認

rubyuser01:~/workspace $ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]

1.2. Railsのバージョンを確認→インストールされていない

rubyuser01:~/workspace $ rails -v
bash: rails: command not found

1.3. OSのバージョンを確認

rubyuser01:~/workspace $ cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

1.4. gemのバージョンを確認

rubyuser01:~/workspace $ gem -v
2.5.1

1.5. gem install でrailsをインストールする

rubyuser01:~/workspace $ gem install rails
Fetching~
Successfully~
:
37 gems installed

1.6. Railsのバージョンを確認

rubyuser01:~/workspace $ rails -v
Rails 5.0.0.1

1.7. Ruby on Railsのプロジェクトを作成する

rubyuser01:~/workspace $ rails new demo
      create
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
          :
Installing web-console 3.3.1
Using rails 5.0.0.1
Installing sass-rails 5.0.6
Bundle complete! 15 Gemfile dependencies, 63 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted
rubyuser01:~/workspace $

1.8. フォルダを確認

rubyuser01:~/workspace $ ls -ltr
total 8
-rw-rw-r--  1 ubuntu ubuntu  948 May  2 10:49 README.md
drwxr-xr-x 12 ubuntu ubuntu 4096 Aug 30 02:12 demo/
rubyuser01:~/workspace $ cd demo/
rubyuser01:~/workspace/demo $ ls -ltr
total 64
-rw-r--r--  1 ubuntu ubuntu  130 Aug 30 02:10 config.ru
-rw-r--r--  1 ubuntu ubuntu  227 Aug 30 02:10 Rakefile
-rw-r--r--  1 ubuntu ubuntu  374 Aug 30 02:10 README.md
-rw-r--r--  1 ubuntu ubuntu 1729 Aug 30 02:10 Gemfile
drwxr-xr-x 10 ubuntu ubuntu 4096 Aug 30 02:10 app/
drwxr-xr-x  2 ubuntu ubuntu 4096 Aug 30 02:10 db/
drwxr-xr-x  5 ubuntu ubuntu 4096 Aug 30 02:10 config/
drwxr-xr-x  2 ubuntu ubuntu 4096 Aug 30 02:10 log/
drwxr-xr-x  4 ubuntu ubuntu 4096 Aug 30 02:10 lib/
drwxr-xr-x  2 ubuntu ubuntu 4096 Aug 30 02:10 public/
drwxr-xr-x  8 ubuntu ubuntu 4096 Aug 30 02:10 test/
drwxr-xr-x  3 ubuntu ubuntu 4096 Aug 30 02:10 vendor/
drwxr-xr-x  3 ubuntu ubuntu 4096 Aug 30 02:10 tmp/
-rw-r--r--  1 ubuntu ubuntu 4343 Aug 30 02:12 Gemfile.lock
drwxr-xr-x  2 ubuntu ubuntu 4096 Aug 30 02:12 bin/
rubyuser01:~/workspace/demo $

1.9. Webサーバを起動(WEBRick)

rubyuser01:~/workspace/demo $ rails server -b 0.0.0.0 -p 8080
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.0-p0), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:8080
Use Ctrl-C to stop

1.10. ブラウザで確認
(Preview Running Apllication ボタンにてIDE内にブラウザタブが開かれる)

スクリーンショット 2016-08-30 14.14.11.png

1.11. Ctrl+CでWebサーバを停止する

2.. Ruby でMVC開発

2.1. Controllerクラスを作成

rubyuser01:~/workspace/demo $ rails generate controller top index

2.2. ControllerとViewが作成されたことを確認

rubyuser01:~/workspace/demo $ ls -l app/controllers/top_controller.rb 
-rw-r--r-- 1 ubuntu ubuntu 66 Aug 30 04:12 app/controllers/top_controller.rb
rubyuser01:~/workspace/demo $ ls -l app/views/top/index.html.erb 
-rw-r--r-- 1 ubuntu ubuntu 66 Aug 30 04:12 app/views/top/index.html.erb

2.3. ControllerとViewの中身を確認

rubyuser01:~/workspace/demo $ cat app/controllers/top_controller.rb 
class TopController < ApplicationController
  def index
  end
end
rubyuser01:~/workspace/demo $ cat app/views/top/index.html.erb 
<h1>Top#index</h1>
<p>Find me in app/views/top/index.html.erb</p>

2.4. viコマンドにてconfig/routes.rbを編集する(以降よく編集する)

rubyuser01:~/workspace/demo $ vi config/routes.rb 
Rails.application.routes.draw do
-  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
+  root "top#index"
end

2.5. routes.rbの中身を確認

rubyuser01:~/workspace/demo $ cat config/routes.rb
Rails.application.routes.draw do
  root "top#index"
end
rubyuser01:~/workspace/demo $ 

2.6. Webサーバを起動してブラウザで確認

  • viewのapp/views/top/index.html.erb が表示されていることを確認
スクリーンショット 2016-08-30 13.56.20.png
- →app/views/top/index.html.erbの内容を編集して、ブラウザの更新を行うと即座に反映されることを確認
- <p>〜</p>の文言に、「◆」を追加
スクリーンショット 2016-08-30 13.51.32.png
- →config/routes.rbの「root "top#index”」を「root "top#index2”」に編集して、ブラウザの更新を行うと即座に反映されエラーとなる。
スクリーンショット 2016-08-30 13.50.21.png
- →Controllerのapp/controllers/top_controller.rb のAction(メソッド)「index」を「index2」に編集して、さらにエラーとなる※
スクリーンショット 2016-08-30 13.54.06.png

※ indexは初めから備わっている予約語のため

続く

Railsでmodelを作成(SQLite)

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?