2016-01-08 39 views
0

我试图在提交时显示我的某个方法的Flash消息,它会保存并显示Flash消息,但它不会这么做。flash [:notice] not showing rails 4

class PromotionsController < ApplicationController 
    def newsletter_signup 

    @subscriber = Subscriber.new(params[:subscriber]) 
    if params[:subscriber][:email].present? 
     @subscriber.save 
     redirect_to :back 
     flash[:notice] = "Thank you. You have subscribed to our newsletter" 
    end 
    end 
end 

我在application.html.erb

<div class="row "> 
    <div class="icon-send col-sm-4 col-lg-4 hidden-xs col-md-4 col-xs-4"> 
    <span class="text-center subscribeLabel">Subscribe to Our Newletter</span> 

</div> 
<%= form_for(Subscriber.new, url: newsletter_signup_url, :validate => true) do |person_form| %> 
    <div class="col-md-6 col-sm-6 col-xs-7"> 
     <%= person_form.text_field :email, id: "email", class: "SubscibenowText", placeholder: "Enter Your Email Address" %> 
    </div> 
    <div class="col-md-2 col-sm-2 col-xs-5 SubRelative"> 
     <button type="submit" class="pull-right btn btn-default subscribebtn"> 
     GET STARTED 
     </button> 
    </div> 
    <% end %> 
</div> 

观点,我不知道该做些什么,因为我已经尝试了不同的选择,但都无济于事。我将不胜感激,谢谢。

回答

1

我认为这是因为您的页面在将消息分配到flash[:notice]之前正在重定向。

所以,在你的PromotionsController移动你的flash[:notice]行之前redirect_to

而我看不到你在视图中显示的是哪里。

1

你需要太显示提示信息:

<% unless flash.empty? %> 
    <div class="row flash_msg"> 
    <% flash.each do |name, message| %> 
     <div class="text-center alert <%= flash_class(name) %>"> 
     <button class="close" data-dismiss="alert" type="button">&times;</button> 
     <i class="<%= flash_icon(name) %>"></i> 
     <%= message %> 
     </div> 
    <% end %> 
    </div> 
<% end %> 

我这样做是在application.html.erb显示提示信息。 你可以把它放在任何div的application.html.erb文件中。

并在控制器方法中的redirect_to(:back)之前放置flash [:notice]消息。

希望这会有所帮助!

0

redirect_to :back使用javascript:history.back()它不会在您的闪存中设置必要的消息来显示。请尝试使用redirect_to other_page_path。希望有帮助