我按照这篇文章https://reinteractive.com/articles/how-to-create-flash-messages-in-Rails-7使用tailwind添加漂亮的flash消息,这让我进入了视图组件。除了 :type 之外,一切都有效。
如果我添加一条闪现消息,如示例所示:
def home
flash[:message] = "Welcome to the home page!"
flash[:type] = :notice
end
显示:
这是组件.html.erb:
<div class='relative flex px-4 py-3 space-x-2 border <%= color_classes %>' role='alert'>
<strong class='font-bold'><%= message %></strong>
<span class='pl-5'>
<%= heroicon 'x-mark', options: { 'data-action': 'click->flash-message#close', class: 'w-6 h-6 fill-current', role: 'button' } %>
</span>
</div>
这是渲染的:
<div data-controller="flash-message" class="inset-0 p-6 items-start justify-end">
<div class="flex flex-col items-center justify-center">
<div class="relative flex px-4 py-3 space-x-2 border bg-gray-100 border-gray-400 text-gray-700" role="alert">
<strong class="font-bold">Welcome to the home page!</strong>
<div class="relative flex px-4 py-3 space-x-2 border bg-gray-100 border-gray-400 text-gray-700" role="alert">
<strong class="font-bold">notice</strong>
帖子中是否有错误或者过去几个月有更新吗?
这没有任何意义:
flash[:message] = "Welcome to the home page!"
flash[:type] = :notice
应该是:
flash[:notice] = "Welcome to the home page!"
如果你检查
flash
,你可以看到你正在迭代的内容:
#<ActionDispatch::Flash::FlashHash:0x00007f9ede9208b8
@discard=#<Set: {}>,
@flashes={"notice"=>"Welcome to the home page!"}, # <= this
@now=nil>
<% flash.each do |type, message| %>
# │ └─ "Welcome to the home page!"
# └─ "notice"
<%= render FlashMessage::Component.new(type: type, message: message) %>
<% end %>