LoginSignup
1

posted at

【Ruby】初心者がRuby on Railsをやる!

:star:概要:star:

Ruby初心者の僕がRuby on Railsまでやってみた!

:star:初期セットアップ的な:star:

rails new <APP_NAME>
cd <APP_NAME>
bundle install
bundle update

bundle installをする場所は1つ下の階層でやらなきゃいけない!
エラーが出た際の対処法は、下に掲載しています。

:star:サーバーの起動:star:

ubuntu@ubuntuv:~/デスクトップ/projectX/sample$ rails s

ブラウザの127.0.0.1:3000に行って、これが出ればおっけ。

image.png

コンソールでCtrl + cを押して、サーバーを終了する

:star:モデルの生成:star:

モデルを作ろうと思う

なおRuby on Railsの規約として、「モデル名の先頭は大文字、複数形は禁止」というものがありますので、覚えておきましょう。
https://www.sejuku.net/blog/96900

(僕みたいな初心者は命名規則知らずに、変なの使って恥かきそう...///)

ubuntu@ubuntuv:~/デスクトップ/projectX/sample$ rails generate model Product title:string price:integer

データーベースに反映させるためにコマンドを実行

ubuntu@ubuntuv:~/デスクトップ/projectX/sample$ rails db:migrate

サンプルアプリに入れるためのデータを用意する
初期データーを入れるためには、db/seeds.rbに書き込むといい

db/seeds.rb
products = [
  { title: 'りんご', price: 100},
  { title: 'すいか', price: 1000},
  { title: 'ぱそこん', price: 300}
]

ActiveRecord::Base.transaction do
  products.each do |product|
    Product.create!(product)
  end

end

データーベースにサンプルデーターを入れるには以下のコマンドを実行する。

ubuntu@ubuntuv:~/デスクトップ/projectX/sample$ rails db:seed

ちゃんと登録されたか確認したいときには、こんな感じにするといい
自分の環境ではsqlite3なので、そいつで見ていく

ubuntu@ubuntuv:~/デスクトップ/projectX/sample$ sqlite3 db/development.sqlite3 

SQLite version 3.33.0 2020-08-14 13:23:32
Enter ".help" for usage hints.

sqlite> select * from products;

1|りんご|100|2022-04-18 06:22:23.125149|2022-04-18 06:22:23.125149
2|すいか|1000|2022-04-18 06:22:23.126459|2022-04-18 06:22:23.126459
3|ぱそこん|300|2022-04-18 06:22:23.127236|2022-04-18 06:22:23.127236

sqlite> 

ちゃんとはいってる!

:star:コントローラーを作成する:star:

コントローラーはブラウザとモデルやビューとの仲立ちをする役割です

Webブラウザ「サイトの情報ください!」
コントローラー「おいよっ!、ビューさん、こっちによこして!」

みたいなイメージ

これでProductsというコントローラーが作られる。

ubuntu@ubuntuv:~/デスクトップ/projectX/sample$ rails generate controller Products

コントローラを編集して、アクションを追加!

app/controllers/products_controller.rb
class ProductsController < ApplicationController
  def index
    @products = Product.all
  end
end

:star:ビューの作成:star:

ビューは表面上で、見える場所となる
HTMLのような形式で書けばおっけ!

app/views/products/index.html.erb
<!DOCTYPE html>

<html>
  <head>
    <meta charsrt="utf-8">
    <meta name="viewport" content="wodth=device-width">
    <title>サンプルアプリケーションっ!</title>
  </head>
  <body>
    <h1>商品一覧</h1>
    <table border="1">
      <thead>
        <tr>
          <th>商品名</th>
          <th>金額</th>
        </tr>
      </thead>
      <tbody>
        <% @products.each do |product| %>
          <tr>
            <td><%= product.title %></td>
            <td><%= product.price %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </body>
</html>

:star:ルーティングを設定する:star:

ここのアドレスにアクセスが来たら、このアプリを起動するという感じの部分
shellでいうところのaliasみたいな感じかな(?)

config/routes.rb
Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"

  Rails.application.routes.draw do
    get 'products', to: 'products#index'
  end
end

:star:サーバーを起動してみよう!:star:

ubuntu@ubuntuv:~/デスクトップ/projectX/sample$ rails s

ちゃんと表示できたらおっけ!

:star:参考文献:star:

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
What you can do with signing up
1