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 '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"
# ***** 以上を追加 *****
<ターミナル >
これだけでは一部が日本語化されていない
ビューの日本語化
<app/views/devise/registrations/new.html.erb>
<label>属性の部分は日本語化が適用されているが、それ以外は適用されていないので以下の様に修正(白線部)
引数を与えて翻訳
render部分も修正(該当箇所のみ)
翻訳内容を記述(ja.ymlファイル一部のみ抜粋)
これで日本語化の完成!
最終的なブラウザ表示
<参考>
デザインメモ
・カッコは細字で
ちょっとの「工夫」で変わるデザイン
・助詞は小さく(80%)
吾輩は猫である
・覚えにくい字列(数字など)は区切る
ID: 123 456 7890
・単位は小さく
1,000円
・行間はフォントの150〜200%
・字間はデフォルト(5〜10%)
・基本的に左揃え
・文字サイズ:10px以上
・テキスト:16〜18px
・一行あたり35字以内
・テキスト配色
× #000000
○ #333333
・フォントサイズ:8の倍数(もしくは4の倍数)
・行長:560px以内
・横幅の最小は320pxを考慮
ルーティングをネストしている時のprefix
routes.rb
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)