AddしたColumnにNot Null制約を追加する

カラムにnot nullを追加する場合

<ターミナル >

rails g migration AddNotnullOnモデル名

 

<db/migrate/***>

def change

 change_column_null :モデル名, :対象カラム名, false

end

 

過去にAddColumnToしてあるカラムにnot nullを追加する場合

上記の方法では、rails db:migrate時に

column "name" of relation "users" contains null values

と出てマイグレートされない(userモデルのnameカラムにnot nullを追加したい想定)

直訳すると、「users」モデルの「name」カラムには既に「null」が入っているから、not nullできませんよという意味

AddColumnした場合、デフォルトでnullが入ってしまう為にこのような問題が起こる

解決策としては、カラムにnullではない適当な値を入れて、not null設定をする

<db/migrate/***>

def change

 change_column_null :モデル名, :対象カラム名, false, 0 ←"0"を入れてみた

end

deviseの日本語化で、一部が英語表記

日本語化の流れ

<ターミナル >

 gem 'rails-i18n', '~> 6.0'

 gem 'devise-i18n'

 bundle install

 

<config/application.rb>

 module 〇〇

  class Application < Rails::Application

   config.load_defaults 6.0

   # ***** 以下を追加 *****

   config.i18n.default_locale = :ja

   config.time_zone = "Asia/Tokyo"

   # ***** 以上を追加 ***** 

 

<ターミナル >

 rails g devise:i18n:views

 

f:id:you310:20210709221332p:plain
これだけでは一部が日本語化されていない

 

ビューの日本語化

<app/views/devise/registrations/new.html.erb>

f:id:you310:20210709221843p:plain

<label>属性の部分は日本語化が適用されているが、それ以外は適用されていないので以下の様に修正(白線部)

引数を与えて翻訳

f:id:you310:20210710000553p:plain

render部分も修正(該当箇所のみ
f:id:you310:20210709224455p:plain

翻訳内容を記述(ja.ymlファイル一部のみ抜粋)

f:id:you310:20210710000701p:plain

これで日本語化の完成!

 

最終的なブラウザ表示

f:id:you310:20210710000842p:plain

 

<参考>

www2.metro-cit.ac.jp

ichitasu.com

自己アピールメモ

ポートフォリオでポイントの高いもの

・N+1問題を解決

・トップページにこだわる

・テストコード

・企業の求める技術的な条件を明記する

 

人物像

・アウトプット資料を添付

 勉強会資料、LT登壇資料など

・公式の勉強会に参加してみる

・行動の理由や結果

 ✖️ 〜〜しました

 ○ △△を活かして〜〜しました

・実案件を受けてみる

デザインメモ

・カッコは細字で

 ちょっとの工夫で変わるデザイン

・助詞は小さく(80%)

 吾輩猫である

・覚えにくい字列(数字など)は区切る

 ID: 123 456 7890

・単位は小さく

 1,000

・行間はフォントの150〜200%

・字間はデフォルト(5〜10%)

・基本的に左揃え

・文字サイズ:10px以上

・テキスト:16〜18px

・一行あたり35字以内

・テキスト配色

 × #000000

 ○ #333333

・フォントサイズ:8の倍数(もしくは4の倍数)

・行長:560px以内

・横幅の最小は320pxを考慮

ルーティングをネストしている時のprefix

routes.rb
#--- 略 ---
 
resources :posts do
    resources :comments
end

 

 rails routes
prefix HTTPメソッド パス アクション
post_comments GET /posts/:post_id/comments(.:format) comments#index
  POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
  PATCH /posts/:post_id/comments/:id(.:format) comments#update
  DELETE /posts/:post_id/comments/:id(.:format) comments#destroy

 

<例>showへのページ移動時

パスは「/posts/:post_id/comments/:id(.:format)」

IDの指定が2箇所あるので、引数が2つ必要(どの投稿IDか、どのコメントIDか)

post_comment_path(comment.post.id, comment.id)