嗨,我想在创建流言蜚语时创建一个成功警报,并在验证失败时返回我的主页或放置一个危险警报
我刚刚成功地设置了错误警报。
这是我的八卦主持人:
class GossipsController < ApplicationController
def index
@gossips = Gossip.all
end
def show
@gossip = Gossip.find(params[:id])
end
def new
@error = false
end
def create
@gossip = Gossip.new(title: params[:title], content: params[:content], user: User.find(182))
if @gossip.save
redirect_to root_path
else
@error = true
render "new"
end
end
end这是我对新事物的看法:
<% if @error %>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Error</strong>
<ul>
<% @gossip.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<% end %>
<h2>Create your own gossip !</h2> <br><br>
<%= form_tag url_for(action: 'create'), method: "post" do %>
<%= label_tag 'Title :' %> <br>
<%= text_field_tag 'title'%> <br><br>
<%= label_tag 'Content :' %> <br>
<%= text_area_tag 'content'%> <br><br>
<%= submit_tag "Create Gossip" %>
<% end %>对于成功警报,我也尝试过这样做,但是如果在控制器中放置一个@success = true,在索引视图中放置一个<% if @success %>,则不起作用。我没有任何想法。
告诉我你是否需要我的代码。
我试过用闪光灯,但这不起作用,不管我想要什么,都有错误和成功的警告。
发布于 2020-02-23 10:04:10
创建闪存消息部分应用程序/视图/布局/_flash.html.erb(请给文件名加上下划线,因为它是部分的)
<% flash.each do |key, value| %>
<div class="alert alert-<%= key == "success" ? "success" : "danger" %>">
<%= value %>
</div>
<% end %> 添加部分到您的应用程序/视图/布局/application.html.erb
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
...
<%= render 'layouts/flash' %>
<%= yield %>
</Body>
</html>在你的控制器里
def create
@gossip = Gossip.new(title: params[:title], content: params[:content], user: User.find(182))
if @gossip.save
flash[:success] = 'success'
redirect_to root_path
else
flash[:danger] = @gossip.errors.full_messages[0]
render "new"
end
end发布于 2020-02-23 09:59:47
不要在每个窗体视图中写入,而只需编写通用代码来显示与成功或错误相关的警报,然后从任何控制器中使用:-
## app/views/layouts/application.html.erb
<html>
<body>
<%= render 'layouts/header' %> ## your header layout
<div class="container">
<% flash.each do |key, value| %>
<div class="alert <%= flash_class(key) %>"><%= value.try(:html_safe) %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %> ## your footer layout
</div>
</body>
</html>使用助手可根据您的要求获得正确的引导类:-
module ApplicationHelper
def flash_class(level)
level = level.to_sym
case level
when :notice then "alert-info"
when :success then "alert-success"
when :error then "alert-danger"
when :alert then "alert-warning"
end
end
end然后在控制器中使用上述代码:-
if @gossip.save
flash[:success] = "Your success custom message."
redirect_to root_path
else
err_msg = ""
flash.now[:error] = @gossip.errors.full_messages.map{|msg| err_msg << "#{msg} <br> "}
render "new"
endNote :-重定向时使用闪存,呈现时使用flash.now。
编辑内容:-
在显示之前在错误消息中添加<br>标记,然后在视图页面上添加.html_safe,以显示下一行中的每个错误。这个黑客应该是工作的,用户将能够看到与对象相关的所有错误消息。
flash.now[:error] = @gossip.errors.full_messages.map{|msg| err_msg << "#{msg} <br> "}在望
<% flash.each do |key, value| %>
<div class="alert <%= flash_class(key) %>"><%= value.try(:html_safe) %></div>
<% end %>https://stackoverflow.com/questions/60356913
复制相似问题