Tabetai

提供: wikipokpok
Koresoreから転送)
移動先: 案内検索

準備[編集]

プロジェクトディレクトリ作成 移動

バージョン確認

お名前.com問い合わせ

create application[編集]

% rails new . --css=tailwind --css=sass --javascript=esbuild --database=postgresql
% bin/bundle add tailwindcss-rails
% bin/rails tailwindcss:install
gem 'devise', '~> 4.9', '>= 4.9.2'
% bundle install
% bin/setup

サーバー側

$ bundle install  --without test development

database.yml username:などを確認する (2箇所)

# /config/database.yml

production:
  <<: *default
  database: tabetai_production
  username: jq
  password: <%= ENV["TABETAI_DATABASE_PASSWORD"] %>
# /config/environments/production.rb

  config.assets.compile = true

  config.action_mailer.raise_delivery_errors = true                                      
  config.action_mailer.delivery_method = :smtp
  #host = 'localhost'                                                                    
  host = 'http://onk.pokpok.jp'
  config.action_mailer.default_url_options = { host: host }

データベース作成

$ createdb tabetai_production
$ rails db:migrate RAILS_ENV=production

production[編集]

% $ DISABLE_DATABASE_ENVIRONMENT_CHECK=1 rails db:drop RAILS_ENV=production
本番環境でbin/devを実行する。

RAILS_ENV環境変数を設定して本番環境を指定。

export RAILS_ENV=production

bundleコマンドを使用してGemの依存関係をインストール。

bundle install

プリコンパイルされたアセットを生成。

bundle exec rails assets:precompile

サーバーを起動。

bundle exec rails server

テスト用のデータで開発用のデータをつくる設定[編集]

# db/seeds.rb

puts "\n== Seeding the database with fixtures =="
system("bin/rails db:fixtures:load")

db:rollback[編集]

% rails db:migrate:status
% rails db:rollback
% rails db:rollback STEP=n
% rails db:migrate:down VERSION=**************

gemのアンインストール方法[編集]

$ bundle exec gem uninstall <gem名>

Gemfileから該当のgemの行を削除

$ bundle install

user[編集]

% bundle exec rails g model User email:string
t.string :email, null: false, index: { unique: true } 
% bundle exec rails db:migrate
# app/models/user.rb

  validates :email, presence: true, uniqueness: true

home[編集]

ホームを作る

% rails generate controller Pages home

homeコントローラーはパブリックなので、認証をはずす

# app/controllers/pages_controller.rb

 skip_before_action :authenticate_user!

ルートパス

# config/routes.rb

root to: "pages#home"


user devise[編集]

% bin/rails generate devise:install
アプリケーションの設定に応じて、いくつかの手動のセットアップが必要です:

1. 環境ファイルでデフォルトのURLオプションを定義してください。以下は、開発環境に適したdefault_url_optionsの例です。config/environments/development.rbに追加します:

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

   本番環境では、:hostをアプリケーションの実際のホストに設定する必要があります。

   * すべてのアプリケーションに必要です。 *

2. config/routes.rbでroot_urlを*何か*に定義してください。例:

   root to: "home#index"

   * API専用アプリケーションには必要ありません。 *

3. app/views/layouts/application.html.erbにフラッシュメッセージがあることを確認してください。例:

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

   * API専用アプリケーションには必要ありません。 *

4. カスタマイズするために、Deviseビューをアプリケーションにコピーするには、次のコマンドを実行します:

   rails g devise:views

   * 必須ではありません。 *

1

# config/environments/development.rb
  host = 'http://koresore.pokpok.jp'
  config.action_mailer.default_url_options = { host: host }

2

# app/views/layouts/application.html.erb
<p class="notice"><%= notice %></p>
 <p class="alert"><%= alert %></p>

Modelを作成

$ bin/rails generate devise User
$ bin/rails db:migrate

Viewを作成

$ rails generate devise:views users

view更新

# config/initializers/devise.rb

config.scoped_views = true


ログインしないとアクセスできないようにする

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  before_action :authenticate_user!, unless: :devise_controller?
end

DeviseとOmniauthでGoogle、Twitter、Facebook認証[編集]

gem 'dotenv-rails' 
$ bundle install
$ touch .env
HOST='example.jp'
TWITTER_API_KEY=
TWITTER_API_SECRET=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
FACEBOOK_KEY=
FACEBOOK_SECRET=
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem 'omniauth-google-oauth2'

bundle install後のメッセージ

$ rails g migration AddOmniauthToUsers provider:string uid:string
# config/initializers/devise.rb

config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], scope: 'email', info_fields: 'email', callback_url: "#{ENV['HOST']}/users/auth/facebook/callback"

config.omniauth :twitter, ENV['TWITTER_API_KEY'], ENV['TWITTER_API_SECRET'], scope: 'email', oauth_callback: "#{ENV['HOST']}/users/auth/twitter/callback"

config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], scope: 'email', redirect_uri: "#{ENV['HOST']}/users/auth/google_oauth2/callback"

OmniAuth.config.logger = Rails.logger if Rails.env.development?
$ mkdir app/controllers/users/
$ touch app/controllers/users/omniauth_callbacks_controller.rb

test[編集]

~/tabetai2$ bin/rails generate simple_form:install