首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

怎么在model里使用view helper

2012-09-20 
如何在model里使用view helperclass Glosentry ActiveRecord::Baseinclude ActionView::Helpers::TextHe

如何在model里使用view helper

class Glosentry < ActiveRecord::Base  include ActionView::Helpers::TextHelper    def short_explanation(len=20)     truncate(self.explanation, len)  endend

上面的方法虽然可以用但,逻辑上并不好,因为Model不是helper,所以可以用下面的方法

class Glosentry < ActiveRecord::Base  class GlosentryHelper    include ActionView::Helpers::TextHelper  end    def helper    @h ||= GlosentryHelper.new  end    def short_explanation(len=20)     helper.truncate(self.explanation, len)  endend


  def method_missing(*args, &block)    @h.send(*args, &block)  end

热点排行