我试图在提交时显示我的一个方法的flash消息,它保存并显示flash消息,但它没有这样做。
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>我不确定还能做什么,因为我尝试了不同的选择,但都没有效果。如果能帮上忙我会非常感谢。
发布于 2016-01-08 15:07:35
我认为发生这种情况是因为您的页面在将消息分配到flash[:notice]之前被重定向。
因此,在您的PromotionsController中,将您的flash[:notice]行移到redirect_to之前。
我看不到您在视图中显示此flash消息的位置。
发布于 2016-01-08 15:23:44
您还需要显示flash消息:
<% 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">×</button>
<i class="<%= flash_icon(name) %>"></i>
<%= message %>
</div>
<% end %>
</div>
<% end %>我在application.html.erb中这样做是为了显示闪光消息。您可以将其放入任何div中的application.html.erb文件中。
并在控制器方法中将flash:notice消息放在redirect_to(:back)之前。
希望这能有所帮助!
发布于 2016-01-08 21:35:00
redirect_to :back使用javascript:history.back(),它不会在闪光灯中设置要显示的必要消息。相反,请尝试使用redirect_to other_page_path。希望这能有所帮助
https://stackoverflow.com/questions/34671134
复制相似问题