Rails中Flash
2013-05-31 11:11
225 查看
这个flash与Adobe/Macromedia Flash没有任何关系。
用于在两个actions间传递临时数据,flash中存放的所有数据会在紧接着的下一个action调用后清除。
一般用于传递提示和错误消息。
使用示例: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, 会自动设置。 endend
view代码: show.html.erb<% if flash[:notice] %> <div class="notice"><%= flash[:notice] %></div><% end %>
一共有两种通知:notice与alert,分别表示“提示”和“错误警告”。
flash[:notice]与flash[:alert]有多种写法:
flash.notice=与flash.alert=
flash["notice"]与flash["alert"]
redirect_to时作为参数:alert => “…”, :notice => “…”另外一个还会遇到的是flash.now[],它只对当前action有效,下一个action即无效
flash.now[:message] = “Hello current action”
flash.now[]设置的数据访问方法与其它相同:均为flash['my-key']
原理:
flash.new[]是保存在request中的。alert与notice是保存在session中的, 只是获取数据时添加了删除的逻辑。
注意:
flash[:alert],flash[:notice]一般与redirect_to一起用,而不能与render一起用。
redirect_to是重定向,会重新发起请求,比render多了一次请求。flash[:alert],flash[:notice]只会出现在接下面的一个页面中。
而render是服务器端转发,客户端不会重新发送请求,比redirect_to少了一次请求。所以一旦一起用,结果是接下来两个页面都有flash[:alert],flash[:notice],第三个页面时才会消失。
正确的做法是render搭配flash.now[:alert],flash.now[:notice]一起用
转载自:http://rubyer.me/blog/407/
用于在两个actions间传递临时数据,flash中存放的所有数据会在紧接着的下一个action调用后清除。
一般用于传递提示和错误消息。
使用示例: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, 会自动设置。 endend
view代码: show.html.erb<% if flash[:notice] %> <div class="notice"><%= flash[:notice] %></div><% end %>
一共有两种通知:notice与alert,分别表示“提示”和“错误警告”。
flash[:notice]与flash[:alert]有多种写法:
flash.notice=与flash.alert=
flash["notice"]与flash["alert"]
redirect_to时作为参数:alert => “…”, :notice => “…”另外一个还会遇到的是flash.now[],它只对当前action有效,下一个action即无效
flash.now[:message] = “Hello current action”
flash.now[]设置的数据访问方法与其它相同:均为flash['my-key']
原理:
flash.new[]是保存在request中的。alert与notice是保存在session中的, 只是获取数据时添加了删除的逻辑。
注意:
flash[:alert],flash[:notice]一般与redirect_to一起用,而不能与render一起用。
redirect_to是重定向,会重新发起请求,比render多了一次请求。flash[:alert],flash[:notice]只会出现在接下面的一个页面中。
而render是服务器端转发,客户端不会重新发送请求,比redirect_to少了一次请求。所以一旦一起用,结果是接下来两个页面都有flash[:alert],flash[:notice],第三个页面时才会消失。
正确的做法是render搭配flash.now[:alert],flash.now[:notice]一起用
显示所有notice与alert的helper
application_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 endview中只需添加:<%= display_notice_and_alert %>转载自:http://rubyer.me/blog/407/
相关文章推荐
- rails 国际化、validate校验、flash提示
- 诗歌rails之 关于Rails的错误提示 Rails flash error
- rails flash chart
- 如何在rails中自动隐藏flash message消息
- RailsCasts18 Looping Through Flash 使用循环展现flash
- Rails源代码分析(6):ActionController::Flash
- rails中 flash 和 flash.now的区别
- Rails中使用flash总结
- 解决Ruby On Rails下使用SwfUpload等Flash上传组件取不到Session
- rails Open Flash Chart图表(brown_zhang)
- Rails宝典之第十八式: 循环flash
- rails 国际化、validate校验、flash提示
- rails flash chart
- 关于Rails的错误提示 Rails flash error不消失
- flash视频播放jw player的使用流程(与rails的整合)
- Rails中使用flash总结
- rails中flash[:notice]的问题
- 每天学一点flash (41) 椭圆算法
- Redboot配置从flash引导的linux内核
- Flash Recovery Area(db_recovery_file_dest_size)