LoginSignup
0
1

More than 3 years have passed since last update.

13日目(1):Deviseによるログイン機能付きサイトの作成

Last updated at Posted at 2019-03-24

12日目:12日目:PostgreSQLを用いたログイン機能付きサイトの続き

環境

  • ホストOS: Windows10 Home
  • 仮想環境OS: Ubuntu Bento/Bionic
  • Ruby:2.51
  • Rails: 5.2.2 -主使用gem : devise(参照)
  • DB: PostgreSQL

前回やったこと

  1. nodejsとpostgresqlのインストール
  2. rails new self_univ3 -d postgresql とbundle install (devise)
  3. PostgreSQLのパス設定、DB作成
  4. rails g devise:install
  5. modelsにdeviseを追加。rails g devise Student

今回

  1. controllersとviewsを以前の大学データの方から流用
  2. migrationファイル作成
  3. rooting変更

実作業

controllersフォルダとviewsフォルダのコピー

# cp -r コピーしたいフォルダの場所 ペースト先

migrationファイル作成

rails g migration AddNameToStudents name:string gender:integer age:integer opinion:text
# 実行
 create    db/migrate/20190324043018_add_name_to_students.rb

DBに反映

rails db:migrate

rooting変更

app/confing/routes.rb
# 追加
resources :students
root to: 'students#index'

viewsの変更

app/views/student.html.erb
# 今回不要なExamResultNewのリンク削除
# ログアウトリンクの作成
<% @students.each do |student| %>
      <tr>
        <td><%= student.try(:name) %></td>
        <td><%= student.email %></td>
        <td><%= student.try(:gender) %></td>
        <td><%= student.try(:age) %></td>
        <td><%= student.try(:opinion) %></td>
        <td><%= link_to 'Show', student %></td>
        <td><%= link_to 'Edit', edit_student_path(student) %></td>
        <td><%= link_to 'Destroy', student, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <%= link_to 'Log Out', destroy_student_session_path, method: :delete %>
      </tr>
    <% end %>

コントローラ変更

app/controllers/student_controller.rb
class StudentsController < ApplicationController
  before_action :authenticate_student!

devise-logout.JPG

次からは、このページに、以前の大学データを組み合わせる

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