Rails中Flash
这个flash与Adobe/Macromedia Flash没有任何关系。 controller代码 class PostsController < ActionController::Base ?def create ? # 保存 ? ? post flash[:notice] = "创建POST成功" #可以直接写成notice = "创建POST成功" ? ? redirect_to posts_path(@post) ? ? #上面两行可写成redirect_to posts_path(@post),:notice=> "创建POST成功" ?end ?def show ? ?# 不需要手动设置flash notice到template,会自动设置。 ?end end view代码: show.html.erb <% if flash[:notice] %> ? <div class="notice"><%= flash[:notice] %></div> <% end %> 一共有两种通知:notice与alert,分别表示“提示”和“错误警告”。 另外一个还会遇到的是flash.now[],它只对当前action有效,下一个action即无效 原理: 注意: 显示所有notice与alert的helper
application_helper.rb中添加: ?def display_notice_and_alert ? ?msg = '' ? ?msg << (content_tag :div,notice,:class => "notice") if notice
? ?sanitize msg view中只需添加: <%= display_notice_and_alert %> 转载自:http://rubyer.me/blog/407/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |