登録画面の処理
ログイン、ログアウトの処理
登録情報修正処理
メッセージ作成画面の処理
トーク画面、メッセージ一斉削除処理
テーブルの生成、データ登録(desc,slect)確認
mysql -u root -p
別のターミナル
cd talkapp
config/application.rb
時刻を日本時間でデータベースに保存
...
config.action_record.default_timezone = :local
config.time_zone = 'Tokyo'
...
Gemfileでコメント外す
bcyptライブラリを使うためコメント外す
...
gem 'bcypt', "=> 3.1.7"
...
ライブラリをインストール
bundle install
Userモデルの他にusersテーブルのマイグレーションスクリプトも生成。
rails g model User
app/models/user.rb
passwordのハッシュ化を行うために必要
...
has_secure-password
...
db/migrate/xxxx_create_users.rbの次のコードに返納
passwordのハッシュかを行うためにpassword_digestというフィールド名にします
データベースにテーブルを作成
rails db:migrate
app/controllers/users_controller.rbから次のコードを削除
-class User
-def id
-1
-end
-end
...
app/controllers/users_controller.rbのcreate関数を次のコードに変更
アップロードされた画像のファイル名が過去にアップロードされたものと同じにならないようにするために時間情報をファイル名につけています。
...
...
app/views/layout/_navbar.html.erbを次のようにAuth関連の処理が入った形に変更
ログイン前とログイン後でNavbarの表示が変わる
...
...
app/helpersにsessions_helper.rbを作成し次のコードを入力
ユーザー認証で使用するSessionHelper
module SessionsHelper
def login(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= user.find_by(id: session[:user_id])
end
def logged_in?
!current_user.nil?
end
def loggout
session.delete(:user_id)
@current_user = nil
end
end
app/controllers/application_controller.rbに次のコード追加
SessionsHelperをコントローラで使用できるようにします
...
include SessionsHelper
...
別のターミナル
Webサーバーを動かして動作確認
cd talkapp
rails s
ブラウザhttp://localhost:3000/users/new
名前、Eメール、パスワード、画像を選択して、登録ボタン押す
Navbarの内容が変更されていることを確認
MySQLでデータベースに登録されているか確認
(Navbarより下はダミーデータ、データベースの内容)
app/controllers/sessions-controller.rbのpostLoginとdeleteLogout関数を次のコードに変更
postLoginアクションでは、まずUser.find_by(email: params[:email])でデータベースの中かたemailがログイン画面で入力されたemailのデータを抽出し、
その後authenticateを使ってログイン画面で入力されたpasswordと、抽出されたデータのpasswordをチェックし一致すれば認証OKでトーク画面に進む。
deleteLogoutアクションではlogoutヘルパーメソッドを呼び出してユーザーの状態をログインしていない状態に変更。
...
def postLogin
user = User.find_by(email: params[:email])
if user && user suthenticate(params[:password])
login user
redirect_to posts_path
else
render "login"
end
end
def deleteLogout
logout
redirect_to home_path
end
...
Navbarのログアウトを押してホーム画面
Navbarのログインを押してログイン画面
Eメールとパスワードを入力してログイン、を確認
app/controllers/users_controller.rbのeditとうpだて を次のコードのように変更
登録修正画面で新しいプロフィール画像ファイルが設定せれた場合は、古い画像を削除する
def edit
...
end
Navbarのログアウトを押してホーム画面の名前箇所を押して登録情報修画面
名前、Eメール、パスワード、画像を選択して、動作確認
(Navbarのユーザー名変更、データベースの内容)
rails g model Post
Postモデルの他にpostsテーブルのマイグレーションスクリプトも生成
app/mosels/post.rb
UserとPostは1対多の関係で、Postは多側なのでbelongs_to
db/migrate/xxxx_createposts.rbの次のコードに変更
UserろPostは1対多の関係で、多側のPostにuser_idという外部キーのフィールドが必要
...
t.text :message
integer :user_id
...
rails db:migrate
データベースにテーブルを作成
app/controllers/posts_controller.rbから次のコードを削除
class Post
def set_message(string)
@string = string
end
def message
@string
end
end
...
app/controllers/posts_controller.rbのindexとdelete_all_post関数を次のコードに変更
createアクションでは下記の箇所でログインしているユーザーのidを@post.user_idに設定しています。
これでPostとUserのデータが関連付けられます
@post.user_id = current_user.id
...
def index
@posts = Post.all
end
...
def create
@post = post.new
@post.message = params[:message]
@post.user_id = current_user.id
if @post.save
redirect_to posts_path
else
render :new
end
end
def delete_all_post
Post.delete_all_post
redirect_to_posts_path
end
...
app/views/posts/index.html.erbの次のコード変更
PostとUserはリレーションを持っているため、post.user.imageのような参照ができます
...
"
width=64 height=64>
...
<%= post.user.name %> <%=
post.created_at %>
...