[英]Ruby on Rails 7 + Turbo - Not showing notice message
I am learning Ruby on Rails.我正在学习 Ruby on Rails。 Rails 7 + Turbo stream... I am following a tutorial and I am having a problem with displaying the Notice message.
Rails 7 + Turbo stream...我正在学习教程,但在显示通知消息时遇到问题。
Can you help me understand why I am not getting notice message after I create a new quote?你能帮我理解为什么我在创建新报价后没有收到通知消息吗?
Here is a create action in QoutesController.rb.这是 QoutesController.rb 中的创建操作。 Only when I put this line of code:
只有当我输入这行代码时:
flash[:notice] = "Gets displayed with this line and a refresh... "
before the format.html line, then I will get the notice message after I refresh the page upon creating a quote.在 format.html 行之前,我会在创建报价后刷新页面后收到通知消息。 Only then the message gets displayed.
只有这样才会显示消息。 Can you please help me understand this?
你能帮我理解这个吗?
(Yes, I am using a Devise gem here, Turbo stream, Turbo Rails, Rails 7) (是的,我在这里使用 Devise gem,Turbo stream,Turbo Rails,Rails 7)
Thank you谢谢
def create
@quote = current_company.quotes.build(quote_params)
if @quote.save
respond_to do |format|
flash[:notice] = "Gets displayed with this line and a refresh... " #weird
format.html { redirect_to quotes_path, notice: "Quote was successfully created." }
format.turbo_stream
#debugger
end
else
render :new, status: :unprocessable_entity
end
end
In Rails 7, unrefresh page when you create successfully.在 Rails 7 中,创建成功后取消刷新页面。 So, flash[:notice] not display.
所以,flash[:notice] 不显示。
_flash.html.erb _flash.html.erb
<div class="alert <%= bootstrap_class_for(msg_type) %> alert-dismissible fade show" role="alert">
<div class="container text-center">
<%= message %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</div>
_notices.html.erb _notices.html.erb
<div id="flash">
<% flash.each do |msg_type, message| %>
<%= render partial: "shared/flash", locals: { msg_type: msg_type, message: message } %>
<% end %>
</div>
When you create success -> create views/qoutes/create.turbo_stream.erb当你创造成功时 -> create views/qoutes/create.turbo_stream.erb
<%= turbo_stream.update "flash", partial: "shared/flash", locals: { msg_type: :notice, message: "your message" } %>
// msg_type: :notice, :alert, :error, :success
<%= render 'shared/notices' %>
<%= yield %>
module ApplicationHelper
def bootstrap_class_for(flash_type)
{
success: "alert-success",
error: "alert-danger",
alert: "alert-warning",
notice: "alert-info"
}.stringify_keys[flash_type.to_s] || flash_type.to_s
end
end
and if create failed如果创建失败
ex:前任:
def create
if @object.save
// code
else
render_flash(:alert, full_messages(@post.errors.full_messages))
end
end
In application_controller在 application_controller 中
class ApplicationController < ActionController::Base
protected
def render_flash type, message
render turbo_stream: turbo_stream.update("flash", partial: "shared/flash", locals: { msg_type: type, message: message })
end
def full_messages messages
messages.join("\n")
end
end
=> This is my way. => 这是我的方式。 Hope to help you.
希望能帮到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.