英文:
Rails 7 flash errors not displaying in application.html.erb
问题
I can provide the translation of the code part as requested:
在我的Rails 7应用程序中遇到了警报未显示的问题。
但是,`notices` 是可以显示的。
我在`application.html.erb`中使用`<%= alert %>`来显示警报。
<%= render partial: "shared/navbar" %>
<div class="px-6">
<div class="mt-6"></div>
<div>
<% if notice %>
<div class="notification is-success is-light text-center">
<%= notice %>
</div>
<% end %>
<% if alert %>
<% debugger %>
<div class="notification is-danger is-light text-center">
<%= alert %>
</div>
<% end %>
<%= yield %>
</div>
</div>
</body>
我已经在控制器操作中添加了一个“FAILED”记录器,我在Rails日志中看到了它。这证明我们确实进入了“else”条件,并且在无法创建新对象时渲染了“new”页面。但是,我没有看到警报消息。
def create
@rescue = AnimalRescue.new(rescue_params)
respond_to do |format|
if @rescue.save
format.html { redirect_to animal_rescue_url(@rescue), notice: "成功创建了救援。" }
format.json { render :show, status: :created, location: @rescue }
else
Rails.logger.info "失败"
format.html { render :new, status: :unprocessable_entity, alert: "无法创建救援。" }
format.json { render json: @rescue.errors, status: :unprocessable_entity }
end
end
Please note that the translation is provided for the code part, and the code itself is unchanged.
英文:
Having an issue in my Rails 7 app of alerts not showing up.
notices
, however, are.
I am displaying alerts in application.html.erb
using <%= alert %>
<%= render partial: "shared/navbar" %>
<div class="px-6">
<div class="mt-6"></div>
<div>
<% if notice %>
<div class="notification is-success is-light text-center">
<%= notice %>
</div>
<% end %>
<% if alert %>
<% debugger %>
<div class="notification is-danger is-light text-center">
<%= alert %>
</div>
<% end %>
<%= yield %>
</div>
</div>
</body>
I have added a "FAILED" logger to my controller action, which I am seeing in the Rails logs. This proves to me that we are indeed falling into the "else" condition, and the new
page is being rendered upon failure to create a new object. However, I do not see the alert message.
def create
@rescue = AnimalRescue.new(rescue_params)
respond_to do |format|
if @rescue.save
format.html { redirect_to animal_rescue_url(@rescue), notice: "Rescue was successfully created." }
format.json { render :show, status: :created, location: @rescue }
else
Rails.logger.info "FAILED"
format.html { render :new, status: :unprocessable_entity, alert: "Unable to create rescue." }
format.json { render json: @rescue.errors, status: :unprocessable_entity }
end
end
end
答案1
得分: 1
你过于复杂化了它。要更简单和更灵活,只需使用相同的逻辑来显示各种闪烁消息,唯一需要更改的是单个CSS类:
<% flash.each do |type, msg| %>
<%= tag.div msg,
class: [
{
notice: 'is-success',
alert: 'is-danger'
# 等等
}.fetch(type.to_sym, type.to_s),
"notification",
"is-light",
"text-center"
]
%>
<% end %>
毕竟,flash只是一个包含字符串的简单散列对象。保持简单。
英文:
You're overcomplicating it. It will be waaay easier and more flexible to just use the same logic to display all kinds of flash messages as the only thing that needs to change is a single CSS class :
<% flash.each do |type, msg| %>
<%= tag.div msg,
class: [
{
notice: 'is-success',
alert: 'is-danger'
# etc
}.fetch(type.to_sym, type.to_s),
"notification",
"is-light",
"text-center"
]
%>
<% end %>
The flash is after all just a simple hash like object that contains strings. KISS.
答案2
得分: 1
format.html { render :new, status: :unprocessable_entity, alert: "无法创建救援。" }
# NOTE: 不是渲染选项 ^^^^^^
:alert
和 :notice
是用于 redirect_to
方法的选项,它们设置相应的 flash
消息:
flash[:alert] = "无法创建救援。"
另外,flash
只会在重定向后的下一个请求中显示。要在当前请求中显示闪现消息,您需要使用 flash.now
:
format.html do
flash.now.alert = "无法创建救援。"
render :new, status: :unprocessable_entity
end
如果您的表单位于 Turbo 框架内,您需要在框架内显示警报。
英文:
format.html { render :new, status: :unprocessable_entity, alert: "Unable to create rescue." }
# NOTE: not a render option ^^^^^^
:alert
and :notice
are options for redirect_to
method, which set the corresponding flash
message:
flash[:alert] = "Unable to create rescue."
Also, flash
will only display on the next request after the redirect. To display a flash message in the current request you have to use flash.now
:
format.html do
flash.now.alert = "Unable to create rescue."
render :new, status: :unprocessable_entity
end
In case your form is inside of the turbo frame, you'll need to display an alert from within the frame.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论