Rails中使用flash总结
转载:http://rubyer.me/blog/407/ 这个flash与Adobe/Macromedia Flash没有任何关系。 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的helperapplication_helper.rb中添加: def display_notice_and_alert msg = '' msg << (content_tag :div,notice,:class => "notice") if notice msg << (content_tag :div,alert,:class => "alert") if alert sanitize msg end view中只需添加: <%= display_notice_and_alert %> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |