20
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

railsでユーザー機能を作成する

Last updated at Posted at 2017-08-13

仕様書

webサービスにユーザーの登録、ログイン、編集で必要な機能を作成していく。

  • ユーザーの一覧作成
  • ユーザーの詳細ページ
  • ユーザーの新規登録
  • ユーザーの情報編集
  • ユーザーのログイン(セッションなどの設定)

ユーザー機能の準備

テーブルとモデルの作成

rei
rails g model カラム名:データ型
rails db:migrate

上記でまずモデルとテーブルを作成する。今後テーブルのみ作りたい事が出た場合はこのやり方だと一緒にモデルも作成されるのでダメ。

この処理行なわれるのは

  • db/migrateフォルダにmigrationファイルの作成
  • modelフォルダにmodel作成

バリデーションをかける

バリデーションというのはユーザーが情報を入力する際に間違っていたらはじく仕組みのこと。

rei
validates :カラム名, {presence: true, uniqueness: true}

上記のようにカラム名に対してどういった種類のバリデーションをかけるのかをモデルで設定する。

ユーザー一覧の作成

コントローラの作成

rei
rails g controller コントローラ名 アクション名

これで

  • controllerの作成
  • ビューファイルの作成

が行なわれる。

ルートの設定

ルートファイルに

rei
get "URL" => "コントローラ名#アクション名"

を設定

コントローラファイルにユーザーのデータを全て引っ張ってくる変数の設定

rei
class UsersController < ApplicationController
  def index
    @users = User.all
  end
end

ビューファイルにてeach文で繰り返しでユーザー情報表示

rei
<!-- 配列@usersの要素を1つずつ取り出し変数userに代入 -->
<% @users.each do |user| %>
  <!-- 繰り返したい処理を記述する -->
<% end %>

ユーザー詳細ページの作成

ユーザー一覧から飛べる詳細ページの作成をしていきます。

作りたいモノ

  • 一覧ページからリンクで飛べる
  • /users/showで見れる

ルーティングの作成

routes.rb
get "users/:id" => "users#show"

users/:idにアクセスされた時にusersコントローラのshowアクションに問い合わせするように設定

アクションの設定

users_controller.rb
def show
  @user = User.find_by(id: params[:id])
end

showアクションを設定し、Userクラスを用いてユーザー情報を引っ張ってくる。これで対象のidのユーザー情報を@userに入れている。@がついているのはアクションで設定した変数をviewで使う際に必要なため。

showファイルの作成

viewにshowファイルを新規で作成する。

ユーザー登録機能の作成

登録ページの作成

まずviewを作成していく。

ルートの設定

routes.rb
  get "signup" => "users#new"

これでドメイン/sigupのURLが打ち込まれた際にusersコントローラのnewアクションを開くようになる。

コントローラ、ビューの作成

users_controller.rb
def new
end

コントローラにnewアクションを作成し、new.html.erbも新規で作成。

ユーザー情報の登録

ルートの設定

routes.rb
post "users/create" => "users#create"

DBを書き換えるためのルートなのでpostを用いている。これでformからusers/createが問い合わせされた際にusersコントローラのcreateアクションを問い合わせる。

ビューのformタグの設定

new.html.erb
<%= form_tag("送信先URL") do %>
  フォームの中身
<% end %>

inputタグについて

new.html.erb
<%= form_tag("送信先URL") do %>
  <input name = "">
<% end %>

name属性を設定することでparamsで受け取れるようになる。

new.html.erb
def create
  @user = User.new(name: params[:name], email: params[:email])
  @user.save
  redirect_to("/users/#{@user.id}")
end

form_tagからcreateアクションに問い合わせを行い、

  • @userに登録された情報を入れる
  • @user.saveでDBに保存
  • redirect_toでユーザー詳細ページに飛ばす

を設定。

ユーザー登録時にログイン状態にする

ユーザー登録がすんだらログイン済みにしておかないと再度ログインしなければいけません。登録のアクションに

rei
def create
 @user = User.new(
      name: params[:name],
      email: params[:email],
      image_name: "default_user.jpg",
      password: params[:password]
    )
    if @user.save
      session[:user_id] = @user.id
      flash[:notice] = "ユーザー登録が完了しました"
      redirect_to("/users/#{@user.id}")
    else
      render("users/new")
    end
  end

ユーザー情報編集機能

編集画面の作成

routes.rb
get "users/:id/edit" => "users#edit"

で作成。viewの作成の場合はget。form_tagなどでDBを変更する場合はpost

users_controller.rb
def edit
  @user = User.find_by(id: params[:id])
end

アクションはそのviewでしか効かないので@userの定義をもっかいする。

20
26
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
20
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?