2014-01-18 34 views
0

我试图在下一个两个示例中显示一条消息,然后再转到另一个视图。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> 
+1

看起来非常标准的给我。我始终使用带有“重定向”的第一个版本。您需要发布控制器代码,视图代码以及通过重写Javascript所做的任何更改。通过“返回到我的路径”,你的意思是点击后退按钮,页面没有被刷新? – Beartech

+0

只需添加我的控制器代码和查看代码。我不重写任何Javascript。 –

+0

你可以显示'app/views/layouts/application.html.erb'的视图吗?我假设这是你渲染Flash消息的地方? – Beartech

回答

1

使用flash.now而不要使用闪光灯。

flash变量旨在用于重定向之前,并且它在一个请求的结果页面上保留。这意味着,如果我们不重定向,而是简单地呈现一个页面,则该消息将持续两个请求:它出现在呈现的页面上,但仍在等待重定向(即第二个请求),因此消息将再次出现,如果你点击一个链接。

为了避免这种奇怪的行为,在渲染而不是重定向时我们使用flash.now而不是flash。

flash.now对象用于在呈现的页面上显示Flash消息。根据我的假设,如果你发现一个你不期望的随机闪光消息,你可以通过用flash.now替换flash来解决它。

希望帮助:)

+0

在尝试我在我的问题中提到的内容之前,我尝试了flash.now。闪光消息未显示。 flash.now适用于渲染语句。我主要用它来显示错误信息。 –

0

flash.discard [:符号] = “值” 不会做你认为它。

基本上,该呼叫由控制器这样解释的:

flash.discard[:symbol] # discards this flash. It doesn't exist yet, but it doesn't throw an exception. 
flash[:symbol] = "value" # now assigns the value to the key. 

所以这就是为什么这两个闪光灯[:符号]和flash.discard [:符号]做同样的事情。

至于为什么你没有得到闪回回声,这是一个棘手的问题。有可能您的救援声明1)首先执行闪光灯,然后再次返回闪光灯。让你的救援​​声明返回其他东西,看看会发生什么?

或重写你的声明是这样的:

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." 
    redirect_to maint_mailing_list_path 

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." 
    redirect_to maint_mailing_list_path 
end 
0

我通常做的提示信息是这样的:

<% flash.each do |key, value| %> 
    <div class='flash' id='<%= key %>'> 
     <%= value %> 
    </div> 
<% end %> 
相关问题