我试图在下一个两个示例中显示一条消息,然后再转到另一个视图。Rails 4 - Flash消息在离开页面并返回多次后保留
flash[:success] = "Here is my flash message."
redirect_to my_path
flash.discard[:success] = "Here is my flash message."
redirect_to my_path
在这两种情况下,显示my_path时都会按预期显示消息。当我离开那个视图,然后返回到my_path,仍然显示flash消息。我可以显示另一个视图,然后在我的应用程序中多次返回my_path,并且仍然显示消息。
我知道使用flash.now
将不起作用,因为我没有执行渲染命令。
我不知道如何改变这种情况,因为我在网上发现的关于这个的每篇文章都提到我在使用redirect_to时应该使用flash.discard
。我没有发现任何说flash.discard
在Rails 4中被弃用的东西。也许在Rails 4中有另一个选项,我不知道。
任何帮助,将不胜感激。
UPDATE 2014年1月18日上午8:50 CST
查看
<% provide(:title, @page_title) %>
<div class="row-fluid" style="background-color: #ffffff;">
<div class="span12" align="center">
<div class="contact-class">
<div class="contact-form">
<div class="row-fluid">
<div class="span8 offset2">
<p>By joining the 4Dcube3Dcross ™ Mailing List you will receive announcements about new 4Dcube3Dcross ™ website or web app features, new products and promotions.</p>
</div>
</div>
<div class="row-fluid">
<div class="span6">
<div class="headings">JOIN</div>
<%= form_tag maint_mailing_list_path, method: 'get' do %>
<p style="font-weight: bold;">To Add a new Mailing List entry, enter a Valid Email Address below and click the button:</p>
<%= text_field_tag :list_email, params[:list_email], :minlength => 2, :maxlength => 70, placeholder: "Valid Email Address (max. 70)" %><br><br>
<div><span valign="center"><%= submit_tag "Join Our Mailing List", class: "btn btn-medium btn-custom" %></span></div>
<% end %>
</div>
<div class="span6">
<div class="headings">EDIT/DELETE</div>
<%= form_tag mailing_list_maint_mailing_lists_path, method: 'get' do %>
<p style="font-weight: bold;">To Edit/Delete an existing Mailing List entry, enter a Valid Email Address below and click the button:</p>
<%= text_field_tag :edit_email, params[:edit_email], :minlength => 2, :maxlength => 70, placeholder: "Valid Email Address (max. 70)" %><br><br>
<div><span valign="center"><%= submit_tag "Edit/Delete Mailing List Entry", class: "btn btn-medium btn-custom" %></span></div>
<% end %>
</div>
</div>
</div>
</div>
</div>
</div>
控制器
def maint_mailing_list
@page_title = "4Dcube3Dcross Mailing List"
if params[:commit]=='Join Our Mailing List'
case
when params[:list_email].blank? || params[:list_email] !~ /\A[\w+\-.][email protected][\w\-.]+\.[a-z]+\z/i
flash.now[:error] = "Email must be a valid email address. Please try again with a valid email address."
render "maint_mailing_list"
when MailingList.find_by(email: params[:list_email]).present?
flash.now[:error] = "The email address entered is already on the mailing list. Please try another email address."
render "maint_mailing_list"
else
begin
HyperCubeOfLoveMailer.add_mailing_list_email(params[:list_email]).deliver
flash.discard[:success] = "We want to confirm that you actually requested to be added to the 4Dcube3Dcross Mailing List. You have been sent an email with instructions on how to add yourself."
rescue Net::SMTPFatalError, Net::SMTPSyntaxError, Net::SMTPAuthenticationError => mailing_lists_error
logger.warn mailing_lists_error
flash.discard[:notice] = "There was a problem with sending your email with instructions on how to add yourself. Please try again."
end
redirect_to maint_mailing_list_path
end
else
render "maint_mailing_list"
end
end
我用flash
并得到了相同的结果。我使用完全相同的逻辑,但将redirect_to从maint_mailing_list_path
更改为home_path
,以防重定向到自身时可能出现问题。但是,当我通过单击链接离开主视图并显示另一个视图时,通过单击链接显示主视图,该信息仍然存在。
UPDATE 2014年1月18日下午3:20 CST
application.html.erb
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= csrf_meta_tags %>
<%= favicon_link_tag "/faviconhypercube.ico" %>
<%= metamagic :description => "The 'HyperCube of Love' Puzzle from 4Dcube3DcrossTM is based on Ephesians 3:18 that tells how WIDE and LONG and HIGH and DEEP is the LOVE of CHRIST in a 4-Dimension world.", :keywords => %w(hypercube love christ jesus four-dimension 4-dimension three-dimension 3-dimension puzzle lightbe lightbecorp cube 4dcube 3dcross cross ephesians pendant jewelry ebook app tesseract dimension) %>
<%= render 'layouts/shim' %>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-37410472-1']);
_gaq.push(['_setDomainName', 'hypercubeoflove.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-46189750-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="container-fluid">
<%= render 'layouts/header' %>
<% flash.each do |key, value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
<%= javascript_include_tag "application" %>
<%= debug(params) if Rails.env.development? %>
</body>
</html>
看起来非常标准的给我。我始终使用带有“重定向”的第一个版本。您需要发布控制器代码,视图代码以及通过重写Javascript所做的任何更改。通过“返回到我的路径”,你的意思是点击后退按钮,页面没有被刷新? – Beartech
只需添加我的控制器代码和查看代码。我不重写任何Javascript。 –
你可以显示'app/views/layouts/application.html.erb'的视图吗?我假设这是你渲染Flash消息的地方? – Beartech