rails 的一些代码片段

  可能会有用的一些代码片段

 

  1.  一个用于显示flash 的helper

  使用rails的时候经常会用到 redirect :back, :flash => { :error => '你看见了一个错误' } , 写一些内部工具, bootstrap, jquery, 算是rails标配了。

  这里写了一个helper 用来方便的自动显示各种flash 回复。

 1 module ApplicationHelper
 2     def show_flash
 3         flash_type_to_class = {'success' => 'alert-success', 'error' => 'alert-danger', 'warning' => 'alert-warning'}
 4 
 5         content_tags = flash.map do |key, msg|
 6             class_type = flash_type_to_class[key]
 7             class_type = 'alert-info' if(class_type.nil?)
 8             content_tag :div, msg, :class => ['alert', class_type].join(' ')
 9         end
10 
11         sanitize content_tags.join
12     end
13 end    

使用的时候只需要在erb 模板内使用: <%= show_flash %> 就可以很好的使用了

 

posted @ 2016-09-08 16:07  海狸先森  阅读(6)  评论(0)    收藏  举报