LoginSignup
12
14

More than 5 years have passed since last update.

Ruby on Rails でCRUD機能を実装するまで

Last updated at Posted at 2016-02-16

CRUD・・・・?

CRUD(クラッド)とは、ほとんど全てのコンピュータソフトウェアが持つ永続性の4つの基本機能のイニシャルを並べた用語。その4つとは、Create(生成)、Read(読み取り)、Update(更新)、Delete(削除)である。

CRUD - Wikipedia

環境

  • Ruby 2.2.3
  • RubyGem 2.5.2
  • Ruby on Rails 4.2.5
  • CentOS6.7
  • gitインストール済み

流れ

  1. rbenvのインストール
  2. rubyのインストール
  3. Ruby on Railsのインストール
  4. rails newでプロジェクト作成
  5. 必要なGemのインストール
  6. scaffoldでModelやらもろもろ作成
  7. rails serverでサーバ起動
  8. CRUD実装確認

※項番5.以降は作業ディレクトリ上(今回はproject)でコマンドを実行して下さい。

手順

1. rbenvのインストール

1.1 git clone rbenv

$ git clone https://github.com/sstephenson/rbenv.git /opt/

1.2 git clone ruby-build

$ mkdir /opt/rbenv/plugins
$ git clone https://github.com/sstephenson/ruby-build.git /opt/rbenv/plugins

1.3 .bashrc 設定

  • .bashrcに下記を追記
$ vim ~/.bashrc
# rbenv global config
export RBENV_ROOT="/opt/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"

2. rubyのインストール

2.1 モジュールインストール

$ yum -y install gcc make openssl-devel libffi-devel ruby-devel readline-devel rubygems sqlite-devel

2.2 rubyインストール

$ rbenv install 2.2.3
$ rbenv rehash
$ rbenv global 2.2.3

2.3 確認

$ rbenv versions
  system
* 2.2.3 (set by /opt/rbenv/version)

$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

$ which ruby
/opt/rbenv/shims/rub

3. Ruby on Railsのインストール

3.1 インストール

$ gem install rails --no-ri --no-rdoc

いかのようにすることでバージョン指定可能

$ gem install rails -v 4.2.5 --no-ri --no-rdoc

3.2 確認

$ rails --version
Rails 4.2.5

4. プロジェクト作成

4.1 railsのプロジェクトを作成

$ rails new project

5. 必要なGemのインストール

5.1 GemfileへインストールするGemを記載

  • なんかエラーが出てしまうので下記を追記してインストールします。
$ vim Gemfile
gem 'therubyracer'

5.2 bundle install

$ bundle install
Installing therubyracer 0.12.2 with native extensions

6. scaffoldでModelやらもろもろ作成

6.1 scaffold

$ rails g scaffold Note title:string content:text

6.2 db:migrate

$ rake db:migrate
== 20160215044755 CreateNotes: migrating ======================================
-- create_table(:notes)
   -> 0.0019s
== 20160215044755 CreateNotes: migrated (0.0020s) =============================

7. サーバ起動

7.1 rails server

  • rails sだとローカルからのアクセスしか許可されない為、下記のようなオプションを指定する。
$ rails s -b 0.0.0.0
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-02-15 13:51:22] INFO  WEBrick 1.3.1
[2016-02-15 13:51:22] INFO  ruby 2.2.3 (2015-08-18) [x86_64-linux]
[2016-02-15 13:51:22] INFO  WEBrick::HTTPServer#start: pid=4901 port=3000

8. CRUD実装確認

8.1 ルーティングの確認

  • URI Patternの部分を{IPAddress}:3000の後ろにつけてアクセスしてあげれば該当ページへアクセスできます。
    • /home/topのページは作成していないのでアクセスできません。
$ rake routes
   Prefix Verb   URI Pattern               Controller#Action
    notes GET    /notes(.:format)          notes#index
          POST   /notes(.:format)          notes#create
 new_note GET    /notes/new(.:format)      notes#new
edit_note GET    /notes/:id/edit(.:format) notes#edit
     note GET    /notes/:id(.:format)      notes#show
          PATCH  /notes/:id(.:format)      notes#update
          PUT    /notes/:id(.:format)      notes#update
          DELETE /notes/:id(.:format)      notes#destroy
 home_top GET    /home/top(.:format)       home#top

8.2 {IPaddress}:3000/notesへアクセスし、CRUDの機能が実装されていることを確認

Create

  • 新規作成

WS000521.png

  • 内容作成

WS000514.png

  • 作成確認

WS000515.png

Read

  • 内容確認選択

WS000522.png

  • 確認結果

WS000516.png

Update

  • 更新選択

WS000523.png

  • 内容更新

WS000524.png

  • 更新結果確認

WS000525.png

Destroy

  • 削除

WS000518.png

  • 削除確認

WS000519.png

  • 削除結果確認

WS000520.png

感想

実はyumかなんかでインストールしたと思われるRuby1.8が入っていたことに気づかず、gemrailsがインストールできずハマっておりました・・・^^;
しかしRailsの便利さにびっくりですね。
応用すればホームページからWebサービスまで簡単に作成できそうですね!
今後はWebサーバにNginxApacheを使ったり、DBにMySQLを使ってみたりして、
なんか簡単なサービスやホームページ的なものを公開していければいいなとか夢見ています!

12
14
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
12
14