我正在为一个名为SchoolApplication
的模型构建一个表单。如果有人没有成功地填写表单,我想在表单上面呈现一点<strong> you messed up</strong>
。目前,我在控制器中有一些与我的表单相对应的逻辑,即:
if @school_application.save
redirect_to relevant_path
else
render html: '<strong>you messed up </strong>'
end
但是,它不是在新页面中呈现这个内容,而是只显示文本(包括HTML标记),而不是将搞乱。
有什么方法可以在呈现之前在页面上显示此消息吗?特别是在为此目的而创建的<div>
中?
发布于 2014-07-30 11:03:23
您可以使用rails flash
变量来完成这个任务,从文档中可以这样做:
class ClientsController < ApplicationController
def create
@client = Client.new(params[:client])
if @client.save
# ...
else
flash.now[:error] = "Could not save client"
render action: "new"
end
end
end
有关更多详细信息,请参阅overview.html#the-flash
还有一个常见的“抓到”,别忘了在你的application.html.erb
里放一个变体
<html>
<!-- <head/> -->
<body>
<% flash.each do |name, msg| -%>
<%= content_tag :div, msg, class: name %>
<% end -%>
<!-- more content -->
</body>
</html>
https://stackoverflow.com/questions/25044801
复制相似问题