0
0

More than 1 year has passed since last update.

新規登録機能を作成する

Posted at

ログイン機能作成する

まずusersテーブルにパスワードカラムを作成することから初めて
それからHTMLで画面を作って、railsで中身を作成していこうかな。
よし始めよう!

MySQLを使ってusersテーブルでパスワードカラムを作成する

mysql> ALTER TABlE users
    -> ADD password varchar(50) AFTER name;
ERROR 1046 (3D000): No database selected
mysql> show databases;
+---------------------------+
| Database                  |
+---------------------------+
| garden_development        |
| garden_review_development |
| garden_review_test        |
| garden_test               |
| information_schema        |
| mysql                     |
| performance_schema        |
| sys                       |
| training                  |
+---------------------------+
9 rows in set (0.00 sec)

なんでだろう
まさかshow datbases;でデータベースを選ばなくてならないから反応しないのかな?

mysql> use garden_development
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from uses
    -> ;
ERROR 1146 (42S02): Table 'garden_development.uses' doesn't exist
mysql> select * from users;
+----+--------------------+
| id | name               |
+----+--------------------+
| 60 | ブイゼル           |
| 61 | a           |
| 62 | a       |
| 63 | a       |
| 64 | あ                 |
| 65 | a       |
| 66 | あ                 |
| 67 | あ                 |
| 68 | あ                 |
| 69 | い                 |
| 70 | あ                 |
| 71 | あ                 |
| 72 | ブイゼル           |
| 73 | ブイゼル           |
| 74 | ブイゼル           |
| 75 | ブイゼル           |
| 76 | 早起き             |
| 77 | a       |
| 78 | a       |
+----+--------------------+
19 rows in set (0.00 sec)

これで変更した。

mysql> ALTER TABLE users
    -> ADD COLUMN password VARCHER(50) AFTER name;

これでなるか?

これでなるか?
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHER(50) AFTER name' at line 2

VARCHER(50) AFTER nameがおかしいらしい。

mysql> ALTER TABLE users ADD password varchar(50) NOT NULL;

ちょっと違いがわからなかったので調べてみて、入力してみた。
出典 https://uxmilk.jp/12612

Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

成功した。

mysql> select * from users;
+----+--------------------+----------+
| id | name               | password |
+----+--------------------+----------+
| 60 | ブイゼル           |          |
| 61 | a          |          |
| 62 | a       |          |
| 63 | a      |          |
| 64 | あ                 |          |
| 65 | a       |          |
| 66 | あ                 |          |
| 67 | あ                 |          |
| 68 | あ                 |          |
| 69 | い                 |          |
| 70 | あ                 |          |
| 71 | あ                 |          |
| 72 | ブイゼル           |          |
| 73 | ブイゼル           |          |
| 74 | ブイゼル           |          |
| 75 | ブイゼル           |          |
| 76 | 早起き             |          |
| 77 | a       |          |
| 78 | a      |          |
+----+--------------------+----------+
19 rows in set (0.00 sec)

何がおかしかったのだろう?
それは後にして先へ行こう。

ログインページを作成する

まずはルーティングから

Rails.application.routes.draw do
  get '/',                       to: 'home#top'
  get '/signup'                  to: 'users#new'
  get 'users/index',             to: 'users#index'
  get 'users/:id',               to: 'users#show'
end
routes.rb
Rails.application.routes.draw do
  get '/',                       to: 'home#top'
  get '/signup',                  to: 'users#new'
  get 'users/index',             to: 'users#index'
  get 'users/:id',               to: 'users#show'
end

SyntaxError

/Users/hyoudoumasatomo/Repos/garden/config/routes.rb:3: syntax error, unexpected local variable or method, expecting `end'
...et '/signup' to: 'users#new'
,を忘れていた。

作動するusersコントローラのloginアクションを作成する

users_controller.rb
class UsersController < ApplicationController
.
.
.
  def new
  end
end

新規登録ページを作成

usersディレクトリにnew.html.erbのファイルを作成する。
テーブルの属性には名前、email、パスワード、登録日時、更新日時を作成する。

new.html
.
.
.
<div>新規登録</div>
  <div>
    <%= form_tag "#" do  %>
      <p>ユーザー名</p>
      <input name="name" value="name">
      <p>メールアドレス</p>
      <input name="email" value="email">
      <p>パスワード</p>
      <input name="password" value="password">
      <input type="submit" value=新規登録>
    <% end %>
  </div>
.
.
.

ブラウザで表示させてみた。

入力欄の中に値が入っている。
なんでかなと思い返してみると
新規登録が失敗したときにその値を返えすために使うことを思い出した。
だからvalueを消す。

new.html
.
.
.
<div>新規登録</div>
  <div>
    <%= form_tag "#" do  %>
      <p>ユーザー名</p>
      <input name="name">
      <p>メールアドレス</p>
      <input name="email">
      <p>パスワード</p>
      <input name="password">
      <input type="submit" value=新規登録>
    <% end %>
  </div>
.
.
.

ログインページへのリンクを作成する

application.html.erbからヘッダーにリンクを書く。

application.html
<body>
    <header>
.
.
.
       <li>
         <%= link_to "新規登録", "/signup" %>
       </li>
.
.
.
    </header>

そういえばテーブルのカラムにemailをついすることを忘れていた。

usersテーブルにemailカラムを追加する

mysql> ALTER TABLE users ADD COLUMN email VARCHAR(50) NOT NULL AFTER name;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from users;
+----+--------------------+-------+----------+
| id | name               | email | password |
+----+--------------------+-------+----------+
| 60 | ブイゼル           |       |          |
| 61 | a           |       |          |
| 62 | a       |       |          |
| 63 | a       |       |          |
| 64 | あ                 |       |          |
| 65 | a      |       |          |
| 66 | あ                 |       |          |
| 67 | あ                 |       |          |
| 68 | あ                 |       |          |
| 69 | い                 |       |          |
| 70 | あ                 |       |          |
| 71 | あ                 |       |          |
| 72 | ブイゼル           |       |          |
| 73 | ブイゼル           |       |          |
| 74 | ブイゼル           |       |          |
| 75 | ブイゼル           |       |          |
| 76 | 早起き             |       |          |
| 77 | a       |       |          |
| 78 | a3       |       |          |
+----+--------------------+-------+----------+
19 rows in set (0.00 sec)

現状 

送信もできない状態
ちょっと休む

0
0
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
0