Module: Bootstrap5RailsExtensions::TurboStreamToast

Defined in:
lib/bootstrap5_rails_extensions/turbo_stream_toast.rb

Overview

Turbo::Streams::TagBuilder へ toast(level, text, …) を追加する拡張

使い方(コントローラ/ビュー共通):

render turbo_stream: turbo_stream.toast(:notice, "保存しました")
render turbo_stream: [ turbo_stream.replace(@record), turbo_stream.toast(:alert, "エラー", autohide: false) ]

Instance Method Summary collapse

Instance Method Details

#toast(level, message_or_record, target: "toast-root", autohide: true, delay: 4000) ⇒ Object

トーストを右上コンテナに積むlevel: :notice | :alert | :error | :warning | :info



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bootstrap5_rails_extensions/turbo_stream_toast.rb', line 10

def toast(level, message_or_record, target: "toast-root", autohide: true, delay: 4000)
  color = case level.to_sym
          when :notice then :success
          when :alert, :error then :danger
          when :warning then :warning
          when :info then :info
          else :secondary
          end
  text = normalize_toast_text(message_or_record)
  html = @view_context.render(partial: "bootstrap5_rails_extensions/toast",
                              locals: { msg: { text: text, color: color }, autohide: autohide, delay: delay })
  append(target, html)
end