「Tep8」の版間の差分
(→Flash メッセージ) |
(→準備) |
||
(同じ利用者による、間の38版が非表示) | |||
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 | ||
− | + | % bin/bundle add tailwindcss-rails | |
− | + | % bin/rails tailwindcss:install | |
gem 'devise', '~> 4.9', '>= 4.9.2' | gem 'devise', '~> 4.9', '>= 4.9.2' | ||
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> | ||
28行目: | 36行目: | ||
Viewの追加 | Viewの追加 | ||
rails generate devise:views users | rails generate devise:views users | ||
+ | |||
+ | view更新 | ||
+ | # config/initializers/devise.rb | ||
+ | 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 == | ||
67行目: | 93行目: | ||
bin/rails generate stimulus removals | bin/rails generate stimulus removals | ||
− | + | <nowiki>// app/javascript/controllers/removals_controller.js | |
− | bin/rails destroy stimulus hello | + | |
+ | import { Controller } from "@hotwired/stimulus" | ||
+ | |||
+ | export default class extends Controller { | ||
+ | remove() { | ||
+ | this.element.remove() | ||
+ | } | ||
+ | }</nowiki> | ||
+ | |||
+ | おまけ HelloController削除 | ||
+ | <nowiki>bin/rails destroy stimulus hello | ||
+ | |||
+ | 削除 | ||
+ | import HelloController from "./hello_controller.js" | ||
+ | application.register("hello", HelloController)</nowiki> | ||
+ | |||
+ | == 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 | % 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 | ||
91行目: | 181行目: | ||
bin/rails db:seed | bin/rails db:seed | ||
− | |||
− | |||
− | |||
== user == | == user == | ||
114行目: | 201行目: | ||
notes_path | notes_path | ||
end</nowiki> | end</nowiki> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
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