在一个控制器中,我有
flash[:error] = "Message"
redirect_to :rootroot由另一个控制器处理,视图具有
<% if flash[:error] %>
<p><%= flash[:error] %></p>
<% end %>但是什么都没有显示出来。我插入了<%=调试程序controller.session %>,这是我得到的结果
"flash"=>#<ActionDispatch::Flash::FlashHash:0x2e79208 @used=#<Set: {}>, @closed=false, @flashes={}, @now=nil>}我做错什么了?
发布于 2012-04-01 22:05:10
更新(2019):根据评论,此答案可能不是最新的。
检查这个问题:Rails: redirect_to with :error, but flash[:error] empty。
通知仅在Rails API中声明:
和:alert默认情况下作为闪存哈希值应用。如果需要设置:error值,可以这样做:
redirect_to show_path,:flash => { :error =>“权限不足!”}
发布于 2014-03-05 07:12:55
我知道这很晚了,但我在Rails 4中也遇到了同样的问题。如果你在redirect_to中使用_url助手,闪光消息将会出现:
def update_post
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to show_post_meta_url, notice: 'Post was successfully updated.' }
else
format.html { render action: 'edit_post' }
end
end
end希望这对某些人有帮助。
发布于 2015-06-30 15:48:27
这就是我在控制器中显示警报和通知的方式
redirect_to user_attachments_path, notice: "The file #{@attachment.name} has been uploaded."当您在当前页面上呈现而不像下面这样重定向时,请使用flash[:error]或flash[:notice]:
if params[:attachment].nil?
flash.now[:alert] = "No file found!"
render "new"
elsehttps://stackoverflow.com/questions/9964386
复制相似问题