controller.rb
def create
flash[:alert] = 'some error message'
turbo_frame_updates
end
private
def turbo_frame_updates
# Define the Turbo Frame(s) you want to update
[
turbo_stream.update("flash_messages") do
render(partial: 'shared/flash_messages', formats: [:html])
end
]
end
_shared/flash_messages.html.erb
<% extend ApplicationHelper %>
<%#= turbo_stream.replace "flash_messages" do %>
<%= turbo_frame_tag "flash_messages" do %>
<% flash.each do |key, value| %>
<div class="alert <%= flash_class(key) %> alert-dismissible fade show" role="alert">
<%= raw(value) %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<% end %>
<% end %>
layouts/application.html.erb
<head>
<%= turbo_stream_from :flash_messages %>
</head>
<body>
<%= render 'shared/flash_messages' %>
</body
helpers/application_helper.rb
module ApplicationHelper
def flash_class(level)
bootstrap_alert_class = {
"success" => "alert-success",
"error" => "alert-danger",
"notice" => "alert-info",
"alert" => "alert-danger",
"warn" => "alert-warning"
}
bootstrap_alert_class[level]
end
end
Sending request in json format and expecting flash_messages frame to show error flash notifications.
getting below response from server:
<turbo-frame id="flash_messages">
<div class="alert alert-danger alert-dismissible fade show" role="alert">
undefined local variable or method `errors' for ActiveStorage::Blob:Class
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</turbo-frame>
turbo frame is present in html
<turbo-frame id="flash_messages"></turbo-frame>
Still not updating in UI