「Tep8」の版間の差分
提供: wikipokpok
(→company) |
(→準備) |
||
(同じ利用者による、間の27版が非表示) | |||
1行目: | 1行目: | ||
[[category:memo|{{PAGENAME}}]] | [[category:memo|{{PAGENAME}}]] | ||
== 準備 == | == 準備 == | ||
+ | [[Rails前バージョン確認|バージョン確認]] | ||
+ | |||
== create application == | == create application == | ||
% rails new . --css=tailwind --css=sass --javascript=esbuild --database=postgresql | % rails new . --css=tailwind --css=sass --javascript=esbuild --database=postgresql | ||
13行目: | 15行目: | ||
% bin/setup | % bin/setup | ||
+ | |||
+ | == テスト用のデータで開発用のデータをつくる設定 == | ||
+ | <nowiki># db/seeds.rb | ||
+ | |||
+ | puts "\n== Seeding the database with fixtures ==" | ||
+ | system("bin/rails db:fixtures:load")</nowiki> | ||
== user == | == user == | ||
19行目: | 27行目: | ||
<nowiki>% bin/rails generate devise User | <nowiki>% bin/rails generate devise User | ||
− | + | Confirmableをコメントアウトを外す | |
% bin/rails db:migrate</nowiki> | % bin/rails db:migrate</nowiki> | ||
32行目: | 40行目: | ||
# config/initializers/devise.rb | # config/initializers/devise.rb | ||
config.scoped_views = true | config.scoped_views = true | ||
+ | |||
+ | == Devise == | ||
+ | config.mailer_sender = 'info@pokpok.jp' | ||
+ | |||
+ | メールプレビュー(リンクがエラーになる) | ||
+ | <nowiki># test/mailers/previews/devise_preview.rb | ||
+ | |||
+ | class DevisePreview < ActionMailer::Preview | ||
+ | def confirmation_instructions | ||
+ | Devise::Mailer.confirmation_instructions(User.new, Devise.friendly_token[0,20]) | ||
+ | end | ||
+ | end | ||
+ | |||
+ | http://localhost:3000/rails/mailers/devise/confirmation_instructions</nowiki> | ||
== home == | == home == | ||
88行目: | 110行目: | ||
application.register("hello", HelloController)</nowiki> | application.register("hello", HelloController)</nowiki> | ||
− | == | + | == container == |
− | rails generate scaffold | + | rails generate scaffold Container name:string |
関連付け | 関連付け | ||
− | % bin/rails generate migration | + | % bin/rails generate migration add_container_reference_to_users container:references |
% bin/rails generate migration add_note_reference_to_name_list note:references | % bin/rails generate migration add_note_reference_to_name_list note:references | ||
nullを許可する | nullを許可する | ||
− | add_reference :users, : | + | add_reference :users, :container, foreign_key: true |
− | belongs_to : | + | belongs_to :container, optional: true |
bin/rails db:drop db:create db:migrate | bin/rails db:drop db:create db:migrate | ||
% rails generate controller Companies new create | % rails generate controller Companies new create | ||
+ | |||
+ | 取り消し | ||
+ | % rails destroy scaffold Company name:string | ||
+ | |||
+ | === 一意制約を追加 === | ||
+ | rails generate migration add_unique_constraint_to_containers | ||
== note == | == note == | ||
108行目: | 136行目: | ||
関連付け | 関連付け | ||
− | % bin/rails generate migration | + | % bin/rails generate migration add_container_reference_to_notes container:references |
+ | |||
+ | == client == | ||
+ | % rails generate scaffold Client note:references name:string kana:string email:string line:integer | ||
+ | |||
+ | == ラベル == | ||
+ | === client label === | ||
+ | rails generate scaffold ClientLabel note:references name:string kind:integer | ||
+ | |||
+ | 中間テーブル | ||
+ | rails generate migration CreateJoinTableClientClientLabel client client_label | ||
+ | |||
+ | == other == | ||
+ | % rails generate scaffold Other client:references name:string kana:string client_name:boolean line:integer memo:text generation:string | ||
+ | |||
+ | == db:rollback == | ||
+ | % rails db:migrate:status | ||
+ | |||
+ | % rails db:rollback | ||
+ | |||
+ | % rails db:rollback STEP=n | ||
+ | |||
+ | % rails db:migrate:down VERSION=************** | ||
− | == | + | == ========== == |
% rails generate scaffold NameList name:string kana:string head:boolean line:integer buddhist_name:string alive:boolean birth:string death:string g_age:string memo:text generation:string | % rails generate scaffold NameList name:string kana:string head:boolean line:integer buddhist_name:string alive:boolean birth:string death:string g_age:string memo:text generation:string | ||
2023年7月5日 (水) 11:29時点における最新版
目次
準備[編集]
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
テスト用のデータで開発用のデータをつくる設定[編集]
# db/seeds.rb puts "\n== Seeding the database with fixtures ==" system("bin/rails db:fixtures:load")
user[編集]
% bin/rails generate devise:install
% bin/rails generate devise User Confirmableをコメントアウトを外す % bin/rails db:migrate
# app/models/user.rb :confirmable追記
Viewの追加
rails generate devise:views users
view更新
# config/initializers/devise.rb config.scoped_views = true
Devise[編集]
config.mailer_sender = 'info@pokpok.jp'
メールプレビュー(リンクがエラーになる)
# test/mailers/previews/devise_preview.rb class DevisePreview < ActionMailer::Preview def confirmation_instructions Devise::Mailer.confirmation_instructions(User.new, Devise.friendly_token[0,20]) end end http://localhost:3000/rails/mailers/devise/confirmation_instructions
home[編集]
ホームを作る
% rails generate controller Pages home
homeコントローラーはパブリックなので、認証をはずす
# ユーザーがコンテンツにアクセスするには認証が必要で、共通のワークスペースに属するコンテンツのみが表示される必要がある # ユーザーは、認証されていない場合でも、ホームページからサインイン フォームに移動できる必要がある # アプリケーション全体のどこでもユーザーが認証されていることを確認する必要がある。 # ApplicationControllerDevise authenticate_user!メソッドで、これを強制できる # app/controllers/application_controller.rb before_action :authenticate_user!, unless: :devise_controller? # コールバックに例外を追加 # app/controllers/pages_controller.rb skip_before_action :authenticate_user!
ルートパス
# config/routes.rb root to: "pages#home"
view更新
# config/initializers/devise.rb config.scoped_views = true
ログイン後のパス
# app/controllers/application_controller.rb def after_sign_in_path_for(resource) notes_path end
Flash メッセージ[編集]
DOM からメッセージを削除する
bin/rails generate stimulus removals
// app/javascript/controllers/removals_controller.js import { Controller } from "@hotwired/stimulus" export default class extends Controller { remove() { this.element.remove() } }
おまけ HelloController削除
bin/rails destroy stimulus hello 削除 import HelloController from "./hello_controller.js" application.register("hello", HelloController)
container[編集]
rails generate scaffold Container name:string
関連付け
% bin/rails generate migration add_container_reference_to_users container:references % bin/rails generate migration add_note_reference_to_name_list note:references
nullを許可する
add_reference :users, :container, foreign_key: true belongs_to :container, optional: true
bin/rails db:drop db:create db:migrate
% rails generate controller Companies new create
取り消し
% rails destroy scaffold Company name:string
一意制約を追加[編集]
rails generate migration add_unique_constraint_to_containers
note[編集]
% rails generate scaffold Note name:string kind:integer line:integer
関連付け
% bin/rails generate migration add_container_reference_to_notes container:references
client[編集]
% rails generate scaffold Client note:references name:string kana:string email:string line:integer
ラベル[編集]
client label[編集]
rails generate scaffold ClientLabel note:references name:string kind:integer
中間テーブル
rails generate migration CreateJoinTableClientClientLabel client client_label
other[編集]
% rails generate scaffold Other client:references name:string kana:string client_name:boolean line:integer memo:text generation:string
db:rollback[編集]
% rails db:migrate:status
% rails db:rollback
% rails db:rollback STEP=n
% rails db:migrate:down VERSION=**************
==========[編集]
% rails generate scaffold NameList name:string kana:string head:boolean line:integer buddhist_name:string alive:boolean birth:string death:string g_age:string memo:text generation:string
validates :name, presence: true
t.string :name, null: false
% bin/rails db:drop db:create db:migrate
% bin/rails test:system
% bin/dev
テストデータ[編集]
テストデータと開発データの同期
# db/seeds.rb puts "\n== Seeding the database with fixtures ==" system("bin/rails db:fixtures:load")
bin/rails db:seed
user[編集]
gem 'devise', '~> 4.9', '>= 4.9.2
% bundle install % bin/rails generate devise:install
bin/rails generate devise User Confirmableをコメントアウトする bin/rails db:migrate
view更新
# config/initializers/devise.rb config.scoped_views = true
ログイン後のパス
# app/controllers/application_controller.rb def after_sign_in_path_for(resource) notes_path end