所以我有两个flash通知,如果邮件成功保存则应该显示一个,如果创建新帖子时出错则应该显示另一个。我实施了一段时间但我刚刚意识到错误闪存没有正确显示。它显示的全部是窗口顶部的红色空白通知,而“成功保存”的通知确实正确显示。
我的控制器我有:
def create
@topic = Topic.new
@topic.name = params[:topic][:name]
@topic.description = params[:topic][:description]
@topic.public = params[:topic][:public]
if @topic.save
redirect_to @topic, notice: "Topic was saved successfully."
else
flash.now[:alert] = "Error creating topic. Please try again."
render :new
end
end
新帖子视图:
<div class="col-md-8">
<%= render partial: 'form', locals: { topic: @topic, post: @post } %>
</div>
</div>
_form.html:
<%= form_for [topic, post] do |f| %>
<% if post.errors.any? %>
<div class="alert alert-denger">
<h4><%= pluralize(post.errors.count, "error") %>.</h4>
<ul>
<% post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_group_tag(post.errors[:title]) do %>
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter post title" %>
<% end %>
<%= form_group_tag(post.errors[:body]) do %>
<%= f.label :body %>
<%= f.text_area :body, rows: 8, class: 'form-control', placeholder: "Enter post body" %>
<% end %>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
答案 0 :(得分:0)
请将它放在application.html.erb文件中。
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
答案 1 :(得分:0)
尝试在flash
中添加application.html.erb
次按摩:
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
<%= value %>
</div>
<% end %>
通常将其置于<%= yield %>