`

如何在model里使用view helper

阅读更多
class Glosentry < ActiveRecord::Base
  include ActionView::Helpers::TextHelper
  
  def short_explanation(len=20) 
    truncate(self.explanation, len)
  end
end

上面的方法虽然可以用但,逻辑上并不好,因为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)
  end
end


  def method_missing(*args, &block)
    @h.send(*args, &block)
  end
分享到:
评论

相关推荐

    Android代码-base-adapter-helper

    Multi Item Layout File Support you can find sample in base-adapter-helper-sample-new.... You only need to focus on the mapping between your view and your model. Get it BaseAdapter Helper is now availab

    Android代码-wehax安卓内部库

    wehax安卓内部库 ... framework 提供一套mvp框架的声明和实现....model里提供了基础bean以及若干常用bean。同时定义了数据返回的接口。 data.annotation声明了数据解析用到的若干标注。 data.helper包含数据解析

    张文钿 Rails Best Practices 幻灯片

    在日前结束的Kungfu Rails大会上,来自台湾的著名Rails人张文钿(ihower)为大家带来了一个关于Rails最佳实践的分享,演讲结束后Rails3的核心开发者Yehuda Katz主动索要幻灯片,其内容之精彩已不言而喻。 会后,...

    CodematicDemoMVC

    没有使用view state和服务器表单控件,可以更方便的控制 应用程序的行为 应用程序通过controller来控制程序请求,可以 提供丰富的url重写 支持测试驱动开发 在团队模式下表现更加出众 关注点分离 高扩展性 ...

    WPF应用程序框架(WAF)v2.5.0.7源码2012825

    WPF应用程序框架(WAF)v2.5.0.7源码 源码描述: WPF应用程序框架(WAF)是一个轻量级的...InfoMan:加入在Common.Presentation搜索盒的控制和使用,在EmailCli??ent通讯录模块。 InfoMan:设计数据添加到通讯录意见。

    CodeIgniter实现更改view文件夹路径的方法

    控制view文件夹路径的代码:system/core/loader.php public function add_package_...array_unshift($this-&gt;_ci_model_paths, $path); array_unshift($this-&gt;_ci_helper_paths, $path); $this-&gt;_ci_view_paths = arr

    CVASP框架 201204C.zip

    为什么ASP只有Controller和View而没有Model呢? VBScript本身不支持类的继承,无法实现真正的MVC(不必要钻牛角尖了!)。 开源使用声明:  您必需保留源代码里类似以下的LOGO注释  并且保留 cvinfo.asp 说明...

    [ASP.NET MVC 小牛之路] 文章系列

    十二、[ASP.NET MVC 小牛之路]12 - Section、Partial View 和 Child Action 十三、[ASP.NET MVC 小牛之路]13 - Helper Method 十四、[ASP.NET MVC 小牛之路]14 - Unobtrusive Ajax 十五、[ASP.NET MVC 小牛之路]...

    CI框架装载器Loader.php源码分析

    顾名思义,装载器就是加载元素的,使用CI时,经常加载的有: $this-&gt;load-&gt;library() $this-&gt;load-&gt;view() ... * 用户加载views和files,常见的函数有model(),view(),library(),helper()  *  * Contro

    mvvmhelpers:所有基于XAML的框架的MVVM,转换器和行为库

    MvvmHelpers是一个跨平台的库,用于使用XAML框架流行的Model-View-ViewModel模式创建应用程序。 该库支持: .NET 4.5 单声道4.5 Windows应用商店应用(WinRT) Windows Phone应用程序(WinPRT) Xamarin.iOS ...

    cakephp手册详细讲解

    Model定义 & 关联关系定义 Controller 控制器 View 视图 Component组件 Helper Cake的全局常量及方法 数据检验 Plugins 插件 ACL控制 数据清理 (YY友情参与) Cake Session Component (papa友情参与) The Request ...

    ASPNET身份管理

    用于查看传递数据的控制器:* ViewBag,ViewData,TempData *在ActionResult中创建的模型实例,作为View()参数传递,在指定的@model app.Models.Person视图中,在其余页面中,我们使用Model.propertiest / ...

    backbone-dom-view:骨干的更好的视图类

    骨干视角 骨干的更好的视图类 该视图类的主要思想是在看起来像这样的配置模板对象中 var View = Backbone . DOMView .... template : { ... "model-event #view-event" : function ( ) { //.. } } } }

    asp.net mvc

    Instead of always displaying all validation errors, the Html.ValidationSummary helper method has a new option to display only model-level errors. This enables model-level errors to be displayed in the...

    ASP.NET Core 1.1 For Beginners: How to Build a MVC Website

    * View Component (To render data in the _Layout view with model data) * Razor syntax (To include server-side code in views) * Bootstrap (Used for styling and to create a responsive design) * LINQ ...

    Ext Js权威指南(.zip.001

    1.2.4 在javascript中使用json / 4 1.2.5 在.net中使用json / 8 1.2.6 在java中使用json / 12 1.2.7 更多有关json的信息 / 15 1.3 ext js 4概述 / 15 1.4 ext js的开发工具的获取、安装与配置介绍 / 18 1.4.1...

    sbnotes:CS48 Spring 2015 SBNotes

    #SBNotes for CS48 导师:迈克·科斯坦佐 作者: Andrew Tran、Ishi Von Meier、Nicholas ... -helpers/: Holds any helper classes used to assist the model, view, and controller classes. -models/: These ar

    史上最全韩顺平传智播客PHP就业班视频,10月份全集

    10-15 4 view helper视图助手 10-15 5 遗留问题解决 10-15 6 zf重新部署 10-15 7 zf controller讲解 10-16 1 model讲解 controller view调用 10-16 2 投票系统讲解1 10-16 3 投票系统讲解2 10-16 4 投票系统讲解3 10...

    史上最全传智播客PHP就业班视频课,8月份视频

    10-15 4 view helper视图助手 10-15 5 遗留问题解决 10-15 6 zf重新部署 10-15 7 zf controller讲解 10-16 1 model讲解 controller view调用 10-16 2 投票系统讲解1 10-16 3 投票系统讲解2 10-16 4 投票系统讲解3 10...

Global site tag (gtag.js) - Google Analytics