commitをまとめたい
pullreqでpush済みの複数コミットを一つにまとめるやり方について残しておく。
コミットIDを調べる
以下のコマンドなどをたたいて、まとめたいコミットより前のコミットIDを調べる。
% git log --oneline e58a089d (HEAD -> feature/develop, origin/develop) foofoo 0c9516e0 barbar fd358d82 bazbaz 4e37f343 bazbaz dc6aa3e5 foofoofoo
この中で以下の二つをまとめたいので一つ下のdc6aa3e5
をメモ。
fd358d82 bazbaz 4e37f343 bazbaz
コミットをまとめる
git rebase -i
とメモしたコミットIDをたたく。
% git rebase -i dc6aa3e5
すると以下のように画面遷移する
そして、まとめたいコミットで新しい方のコミットの先頭のpick
をs
に変更する。
pick 4e37f343 bazbaz pick fd358d82 bazbaz pick 0c9516e0 barbar pick e58a089d foofoo # Rebase dc6aa3e5..e58a089d onto dc6aa3e5 (4 commands) # # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup [-C | -c] <commit> = like "squash" but keep only the previous # commit's log message, unless -C is used, in which case # keep only this commit's message; -c is same as -C but # opens the editor # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified); use -c <commit> to reword the commit message # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. #
変更後↓
pick 4e37f343 bazbaz s fd358d82 bazbaz pick 0c9516e0 barbar pick e58a089d foofoo # Rebase dc6aa3e5..e58a089d onto dc6aa3e5 (4 commands) # ~~~~~~~~~~~~~~~
:wq
で変更を保存して終了すると、さらに画面が遷移する。
# This is a combination of 2 commits. # This is the 1st commit message: barbar # This is the commit message #2: barbar # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # ~~~~~~~~~~~~~~~~
コミットメッセージを編集するので以下のように変える。
# This is a combination of 2 commits. # This is the 1st commit message: squash barbar # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # ~~~~~~~~~~~~~~~~
:wq
で保存して終了して完了。
pushする場合は強制pushしないとされない。
Heroku(Rails 6.~)やってみて詰まったところ
自作アプリをHerokuでデプロイしてみたが途中つまづいたので書く。
本家やQiita見比べながら、思考停止で打ち込んだら、エラーが出たのでそこを中心に残してみる。
Heroku スターターガイド (Rails 6.x) | Heroku Dev Center
【初心者向け】railsアプリをherokuを使って確実にデプロイする方法【決定版】 - Qiita
$heroku create <アプリ名>
後のエラー
$ heroku create foo_foo Name must start with a letter, end with a letter or digit and can only contain lowercase letters, digits, and dashes.
GitHubのリポジトリ名と同じにしようとfoo_foo
みたく書いたらエラー
日本語訳は
「名前は、アルファベットで始まり、アルファベットまたは数字で終わり、小文字、数字、ダッシュのみを含むことができます。」
となるので、この場合アンダーバーがいけなかった。
git push heroku master
後のエラー
本家に書いてある通りに打ったがエラー!!
$ git push heroku master error: src refspec master does not match any error: failed to push some refs to 'https://git.heroku.com/foo-foo.git'
これはmasterをmainに変更していたのでエラーが出ていた。
$ git push heroku main
でいけました。
特定のmigrationファイルだけdownにしたい
migrationファイルの2つ前のファイルをdownさせたかったが、rails db:rollback STEP=2
での戻し方では一つ前もdownしてしまう。
そのため特定のmigrationファイルだけをdownする方法を書く。
バージョンを調べる
rails db:migrate:status
で戻したいファイルの Migration ID を調べる。
今回だと下から2つ目の20210519101457
rails db:migrate:status Status Migration ID Migration Name -------------------------------------------- up 20210423074318 foo up 20210519092400 bar up 20210519101457 baz up 20210602114426 example
downさせる
最後に、特定のmigrationファイルをdownさせるために、
先ほど調べたVERSIONにMigration ID書く。
rails db:migrate:down VERSION=20210602102801
これでdownしたので削除したり、修正したりできる。
A server is already runningで詰んだとき(Rails)
無限ループでlocalサーバーがcontrol C
でも停止しないという場面に遭遇したので再起動の仕方を備忘録として。
やり方
作業していたprojectディレクトリ
にいるとして
\tmp\pids\server.pid
のファイルを削除する。
このファイルはサーバー起動する際に使用しているIDが保存されているので、削除してプロセスを終了させたら再び立ち上げられました。
他の記事だと、ターミナルの再起動や上記のIDをkillコマンドで削除させるなどもありましたが上手くいかなかったのでファイル削除しました。
テンプレートエンジンをslimに変更したい
rails new
後はテンプレートエンジンがerbなのでslimに変更したい。
やり方を備忘録として書き記す。
gemを利用する
Gemfileの一番下に以下を書き、保存して終了。
gem 'slim-rails' gem 'html2slim'
その後、bundleを実行し、gemをインストールします。
% bundle
erb形式ファイルを削除し、slim形式ファイルに変更する
app/views/layout
以下には3つのerbファイルがあるので削除してslimファイルに変更する。
gem環境上でコマンドを実行するのでbundle exec <コマンド>
で実行。
% bundle exec erb2slim app/views/layouts/ --delete
これで完了!!!
画像リンクにして大きく表示させる(Markdown)
画像を大きくして見たいときなどに画像をリンクとして表示させる方法。
[![タイトル](画像のURL)](画像のURL)
メソッドとコールバックでデフォルト値を設定する(Rails)
マイグレーションファイルではなく、メソッドとコールバックでデータのデフォルト値を設定する方法について書きます。
前提
Reportモデルのemotionのデフォルト値を
2
にしたいメソッドを作成
# app/models/report.rb def set_default_emotion self.emotion ||= 2 end
||=
は遅延初期化です
遅延初期化については以下のサイトで確認できます
[初心者向け] RubyやRailsでリファクタリングに使えそうなイディオムとか便利メソッドとか - Qiita
コールバックを設定
# app/models/report.rb after_initialize :set_default_emotion, if: :new_record?
after_initialize
コールバックを使用して、newしたらデフォルト値をセットさせます
しかし、after_initialize
は個々のreport画面に遷移したときにも呼び出されます
そのため、新しくデータ(レコード)を作成する場合のみ呼び出したいのでif: :new_record?
を追加します
after_initializeコールバックは、Active Recordオブジェクトが1つインスタンス化されるたびに呼び出されます。インスタンス化は、直接newを実行する他にデータベースからレコードが読み込まれるときにも行われます。
Active Record コールバック - Railsガイド
確認すると、デフォルト値がセットされていることがわかります。