0
0

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 3 years have passed since last update.

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

Last updated at Posted at 2019-03-21

今回からはgemのdeviseを用いて、パスワード等の機能付きrails appを作成する
今までは、MySQLを用いて、授業を行ってきたが、
Herokuを利用するとのことで、HerokuのデフォルトのPostgreSQLを使う。

#使用環境

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

#実作業
##インストール類
何もせずに、rails new app名 -d postgresqlをすると、エラーが起きるので
###postgresqlのインストール

sudo apt install postgresql
sudo apt install libpq-dev

###nodejsのインストール

sudo apt install nodejs

###リポジトリ一覧を更新

sudo apt upgrade

##app作成段階
-dオプションでDBにPostgreSQLを指定

rails new self_univ3 -d postgresql

###gem deviseをインストール

Gemfile
# 書き込む
gem 'devise'
terminal
bundle install

##PostgreSQLの設定
###adminを作成

sudo -u postgres createuser admin -s

###adminでポスグレにログイン

sudo -u postgres psql

###adminユーザのパスワード設定

\password admin

###ポスグレからquit

\q

###databaseを作成し、DB作成

config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: admin
  password: pass
  host: localhost
rails db:create

##devise関連
devise -github を読んで進める

rails g devise:install
# 実行
Some setup you must do manually if you haven't yet:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:

       config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

     In production, :host should be set to the actual host of your application.

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root to: "home#index"

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

       <p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>

  4. You can copy Devise views (for customization) to your app by running:

       rails g devise:views

###modelsにdeviseを追加

rails g devise Student
# 実行
invoke  active_record
      create    db/migrate/20190321230029_devise_create_students.rb
      create    app/models/student.rb
      invoke    test_unit
      create      test/models/student_test.rb
      create      test/fixtures/students.yml
      insert    app/models/student.rb
       route  devise_for :students

###ポスグレにログイン

rails dbconsole

###テーブルを確認

\dt
# 実行
               List of relations
 Schema |         Name         | Type  | Owner
--------+----------------------+-------+-------
 public | ar_internal_metadata | table | admin
 public | schema_migrations    | table | admin
 public | students             | table | admin

###studentsテーブルのスキーマを確認

\d students

##rootingを確認

rails routes
# 実行
  Prefix Verb   URI Pattern                                                                              Controller#Action
        new_student_session GET    /students/sign_in(.:format)                                                              devise/sessions#new
            student_session POST   /students/sign_in(.:format)                                                              devise/sessions#create
    destroy_student_session DELETE /students/sign_out(.:format)                                                             devise/sessions#destroy
       new_student_password GET    /students/password/new(.:format)                                                         devise/passwords#new
      edit_student_password GET    /students/password/edit(.:format)                                                        devise/passwords#edit
           student_password PATCH  /students/password(.:format)                                                             devise/passwords#update
                            PUT    /students/password(.:format)                                                             devise/passwords#update
                            POST   /students/password(.:format)                                                             devise/passwords#create
cancel_student_registration GET    /students/cancel(.:format)                                                               devise/registrations#cancel
   new_student_registration GET    /students/sign_up(.:format)                                                              devise/registrations#new
  edit_student_registration GET    /students/edit(.:format)                                                                 devise/registrations#edit
       student_registration PATCH  /students(.:format)                                                                      devise/registrations#update
                            PUT    /students(.:format)                                                                      devise/registrations#update
                            DELETE /students(.:format)                                                                      devise/registrations#destroy
                            POST   /students(.:format)                                                                      devise/registrations#create
         rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
  rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
         rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
  update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
       rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create

##localhost/students/sign_upに接続
サインアップ画面が出てきた
devisefirst.JPG

ログアウト、アカウント削除機能を付けないといけない。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?