`

Use helpers in controllers or models

阅读更多
引用
http://snipplr.com/view/2505/use-helpers-in-controllers-or-models


his is an easy to use any helpers that rails provides in any other place besides views and view helpers
Plain TextExpand

  
      # create a new file inside lib/ and call it helpers.rb
   
      # paste the following:
   
       
   
      def help
   
      Helper.instance
   
      end
   
       
   
      class Helper
   
      include Singleton
  
      # look inside ActionView::Helpers to include any other helpers that you might need
  
      include ActionView::Helpers::DateHelper
  
      include ActionView::Helpers::TextHelper
  
      end
  
       
  
      # then in any model or controller:
  
      require 'lib/helpers'
  
       
  
      # to use:
  
      # help.name_of_helper
  
      # EX: help.pluralize 10, "person"


分享到:
评论

相关推荐

    Test-Drive ASP.NET MVC

    10.1 Use SOAP or Take a REST Instead? 10.2 Creating a Web Service 10.3 Publishing to Blogger IV Security and Deployment 11 Security, Error Handling, and Logging 11.1 Applying Additional Security ...

    Professional ASP.NET MVC 5

    Covers controllers, views, models, forms, data annotations, authorization and security, Ajax, routing, ASP.NET web API, dependency injection, unit testing, real-world application, and much more ...

    ASP.NET MVC Framework Unleashed(Stephen Walther)

    - **Controllers and Actions:** These components handle user requests and manage the business logic of the application. - **Views:** Responsible for displaying data to the user. - **Models:** Contain ...

    CI 3 详解 英文版

    4. **辅助函数 (Helpers)**: - 辅助函数提供了一组预定义的功能,比如 URL 辅助函数、HTML 辅助函数等。 - 例如:URL 重写、表单生成等。 5. **库 (Libraries)**: - 库包含一组预定义的类和方法,用于执行特定...

    magento入门文档

    <use>core_setup</use> <use>core_write</use> <use>core_read</use> <use>standard</use> <module>Vendor_HelloWorld_Adminhtml <frontName>helloworld ``` 这段...

    Yii2.0多文件上传实例说明

    use frontend\models\Uploadm; use yii\web\UploadedFile; class FormController extends Controller { public function actionMyfiles() { $model = new Uploadm(); return $this->renderPartial('myfiles', ['...

    rails2-sample

    Models, Views, and Controllers(模型、视图和控制器) MVC是Rails的核心架构之一,这一章节将详细介绍这三个组件的作用和相互关系。模型负责与数据库交互,管理数据;视图用于展示数据给用户;控制器则处理用户...

    hello-android-architecture

    - **Controllers/Interactors**:Clean Architecture中的Use Cases,定义应用的核心功能。 - **Repositories**:数据源管理,可以是本地数据库、网络API或其他数据源。 - **DataSources**:具体的数据操作,例如...

    Ruby on Rail 基础知识 一张纸

    - **controllers**: 控制器文件。 - **application.rb**: 主配置文件。 - **helpers**: 辅助方法文件。 - **application_helper.rb**: 全局辅助方法。 - **models**: 模型文件。 - **views**: 视图文件。 - *...

Global site tag (gtag.js) - Google Analytics