LoginSignup
0
0

More than 5 years have passed since last update.

Railsでscaffoldを使わずに招待フォームを作成しよう1(index部分)

Last updated at Posted at 2019-01-20

説明している動画はこちらです。

モデルを作成しよう

terminal
rails generate model User name:string email:string sex:integer age:integer address:integer attendance:integer opinion:text

データベースにテーブルがないことを確認しよう

terminal
mysql -uroot -p cebu_app_development -e "show tables"

テーブルを作成

terminal
rails db:migrate

テーブルが出来たこと、スキーマ情報を確認しよう

terminal
mysql -uroot -p cebu_app_development -e "show tables"

テーブル情報を確認しよう

terminal
mysql -uroot -p cebu_app_development -e "desc users"

ルーティングを設定しよう

config/routes.rb
get 'users', action: :index, controller: 'users'

コントローラーを作成しよう

現在の場所は、/home/vagrant/cebu_appです。
cebu_appは、railsで作成したフォルダであれば、何でもいいです。
自分の環境と置き換えて、使って下さい。

このコマンドで確認できます。

terminal
pwd
terminal
vi app/controllers/users_controller.rb
app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
    @users = User.all
  end
end

usersディレクトリの作成

terminal
mkdir app/views/users
terminal
vi app/views/users/index.html.erb
app/views/users/index.html.erb
<p id="notice"><%= notice %></p>

<h1>Users</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Email</th>
      <th>Sex</th>
      <th>Age</th>
      <th>Address</th>
      <th>Attendance</th>
      <th>Opinion</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.name %></td>
        <td><%= user.email %></td>
        <td><%= user.sex %></td>
        <td><%= user.age %></td>
        <td><%= user.address %></td>
        <td><%= user.attendance %></td>
        <td><%= user.opinion %></td>
      </tr>
    <% end %>
  </tbody>
</table>

表示するために、MySQLからデータを作成しよう

INSERT INTO `users` (`name`, `email`, `sex`, `age`, `address`, `attendance`, `opinion`, `created_at`, `updated_at`) VALUES ('joji', 'joji@gmail.com', 0, 20, 1, 1, 'test', '2019-01-20 11:11:28', '2019-01-20 11:11:28')
0
0
3

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
0