自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Rails自定義Helper模塊含義講解

開發(fā) 開發(fā)工具
Rails自定義Helper模塊全部被放在app/helpers目錄下,如果我們想染一部分Rails自定義Helper模塊進行全局共享的話又該如何呢?

Ruby on Rails開發(fā)框架中有許多可以自定義的模塊,這些東西可以幫助我們更加方便的應(yīng)用框架編寫代碼。在這里我們將會為大家介紹Rails自定義Helper模塊的相關(guān)含義。#t#

Rails默認為每個controller指定一個Rails自定義Helper模塊,所有的helper都放在app/helpers目錄下 ,但是有些Helper我們希望是全局共享的,一般我們將這些Helper方法都扔在ApplicationHelper模塊里 。其實我們可以在app/helpers目錄下建立我們自定義的Helper模塊,如formatting_helper、path_helper等

  1. # formatting_helper.rb   
  2. module FormattingHelper   
  3. def free_when_zero(price)   
  4. price.zero? ? "FREE" :
     number_to_currency(price)   
  5. end   
  6. def yes_no(bool)   
  7. bool? 'Yes' : 'No'   
  8. end   
  9. end   
  10. # path_helper.rb   
  11. module PathHelper   
  12. def articles_path_for_article(article)   
  13. if article.tip?   
  14. tips_articles_path   
  15. else   
  16. news_articles_path   
  17. end   
  18. end   
  19. def product_path(product)   
  20. if product.kind_of? Book   
  21. book_path(product)   
  22. else   
  23. movie_path(product)   
  24. end   
  25. end   
  26. end   
  27. # formatting_helper.rb module 
    FormattingHelper def free_when_zero(price) 
    price.zero? ? "FREE" : number_to_currency(price)
     end def yes_no(bool) bool? 'Yes' : 'No' 
    end end # path_helper.rb module PathHelper 
    def articles_path_for_article(article) if 
    article.tip? tips_articles_path else news_
    articles_path end end def product_path(product) 
    if product.kind_of? Book book_path(product) 
    else movie_path(product) end end end  

 

要想使用這些Rails自定義Helper模塊,我們只需修改ApplicationController即可

  1. class ApplicationController 
    < ActionController::Base   
  2. helper :formatting, :path   
  3. end   
  4. class ApplicationController 
    < ActionController::Base helper 
    :formatting, :path end   

 

或者直接使用Rails自定義Helper模塊 :all來使用所有的Helper

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2012-05-18 10:52:20

TitaniumAndroid模塊自定義View模塊

2009-07-02 15:31:49

JSP標簽

2009-06-30 10:40:25

JSP自定義標簽

2024-02-20 08:01:08

SQL注入器MyBatis數(shù)據(jù)庫

2020-11-19 10:50:43

ImportPython代碼

2010-02-25 16:14:51

Visual Stud

2015-02-12 15:33:43

微信SDK

2015-02-12 15:38:26

微信SDK

2016-12-26 15:25:59

Android自定義View

2009-12-17 15:56:34

Rails參數(shù)bind

2009-12-14 15:30:43

安裝Ruby on R

2024-01-01 13:27:44

pydoc工具代碼

2011-12-16 14:23:51

Java

2015-01-14 15:06:48

定義相機

2009-06-08 20:13:36

Eclipse自定義控

2022-04-24 15:17:56

鴻蒙操作系統(tǒng)

2021-11-23 15:06:42

Kubernetes 運維開源

2013-04-01 14:35:10

Android開發(fā)Android自定義x

2009-12-16 15:41:10

Ruby on Rai

2009-12-16 17:37:31

Ruby on Rai
點贊
收藏

51CTO技術(shù)棧公眾號