2016-01-08 41 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。希望有幫助