日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Rails自定義Helper模塊含義講解

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

Rails默認(rèn)為每個(gè)controller指定一個(gè)Rails自定義Helper模塊,所有的helper都放在app/helpers目錄下 ,但是有些Helper我們希望是全局共享的,一般我們將這些Helper方法都扔在ApplicationHelper模塊里 。其實(shí)我們可以在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來(lái)使用所有的Helper


分享文章:Rails自定義Helper模塊含義講解
網(wǎng)頁(yè)鏈接:http://www.dlmjj.cn/article/dhshsps.html