009994c3985985809d2d644bd49aa1c2
Wiki 管理
  • 2月02日 01:45 在 在 View 的迴圈裡用 scope 會有效能問題嗎? 回應:

    因為 logic 在 loop 中,所以在 controller 使用就必須預先抓出成陣列然後手動一個個整理,程式碼變複雜。

    而這個情況只是呼叫 scope 所以我覺得這個算是 logic in view 的特例?

  • 1月31日 02:02 在 在 View 的迴圈裡用 scope 會有效能問題嗎? 回應:

    嗨,我寫了一個簡單的測試(http://github.com/lulalala/benchmark) ,有兩個動作,一個是在view裡面呼叫排序的 scope ,另一個是在 controller 裡面呼叫。用

    rake test:benchmark

    測試。結果發現兩者沒有什麼差別。

    Benchmark 應該是把 view 跟 controller 合在一起測試,所以應該沒有測錯吧。也許呼叫 scope 不算是 view 中的 query ,所以沒有效率問題?

  • 12月23日 10:02 在 在 View 的迴圈裡用 scope 會有效能問題嗎? 回應:

    不固定會變動

  • 12月07日 09:01 在 怎樣在 Cells Controller 中呼叫 helper 回應:

    最後找到說明了,helper 方法讓 module 能讓 view 用到,但是不能被 controller 用到。
    要讓 controller 用到,得用 include 還得加一個 controller 方法避免「undefined local variable or method `controller' 」問題(不知道原因)。範例程式碼請看最下方:

    而且我發現要是用 image_tag 指定到 asset 圖片時,在 cell 中呼叫反而不會從 images/ 轉換到 assets/ ,所以會破圖。

    總而言之問題多多,所以決定不這樣用,還是寫在view中。

        include ActionView::Helpers::AssetTagHelper
    
        def star
        image_tag("star-#{@rating}.png", alt:"#{@rating}-Star")
        end
    
        def controller
          parent_controller
        end
    
        helper_method :star
    

  • 12月05日 16:16 在 怎樣在 Cells Controller 中呼叫 helper 回應:

    yar not using it, just wrapping built-in helper logic to keep it DRY

  • 12月05日 14:53 在 怎樣在 Cells Controller 中呼叫 helper 回應:

    主要是想學 draper 把 view 弄的更乾淨,因為有幾個 link_to 要重複些許複雜的邏輯,寫在view便是要兩個if(一個還在attribute裡面)。

    謝謝我明天試試(我試了 http://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html#method-i-helpers 不過不成功,大概cell controller不算是真正的 controller)