2011-07-31 17 views
21

我的模型中存在一個常見方法,因爲它是由我的模型調用的。回顧我的觀點,也需要這種模型方法。爲了做到這一點,我有:模型和幫手中的常見方法

  1. 移動的模型方法對application_helper.rb文件
  2. 我的模型通過我的ActiveRecord模型

功能的頂部添加include ApplicationHelper明智調用application_helper方法, 有用。但這是一個好習慣嗎?

我的模型看起來是這樣的:

class Client < ActiveRecord::Base 
    include ApplicationHelper 
end 
+7

無,這是一個非常糟糕的做法。助手應該是**查看**助手,如果你需要模型中的助手的東西,它可能會混入你的視圖。 –

+0

謝謝!你讓我意識到我需要在我的視圖中調用Client#my_method ...我一直在盯着這段代碼太長... – Coderama

+0

你的函數有什麼依賴關係?例如它是否使用'request'或'params'? –

回答

45

include ApplicationHelper寫入您的模型是不好的做法,因爲ApplicationHelper是一個很好的地方,可以在您的視圖中添加大量的幫助函數。這些函數最終將作爲模型的實例方法導入。這些功能大多與您的型號無關,如果它們取決於paramsrequest之類的功能,則不起作用。這裏有另外兩個選項:

選項1:

你可以只定義Client類中的方法,然後從視圖調用它,就像這樣:

class Client < ActiveRecord::Base 
    def self.my_class_method 
    end 
    def my_instance_method 
    end 
end 

然後在你看來:

<%= Client.my_class_method %> 
<%= @client.my_instance_method %> 

選項2:

lib中創建一個單獨的模塊,並將其包含在所需的位置。文件名應與模塊名稱匹配,以便自動加載。

lib/my_module.rb

module MyModule 
    def my_method 
    end 
end 

在你的模型:

class Client < ActiveRecord::Base 
    include MyModule 
    def other_method 
     my_method 
    end 
end 

包括在ApplicationHelper模塊,使之適用於所有的意見:

module ApplicationHelper 
    include MyModule 
end 

然後在您的視圖你可以輕鬆地稱呼它:

<%= my_method %> 
+0

選項1是我最終做的......我不知道爲什麼我試圖過分複雜的東西...... – Coderama

+5

@Coderama,我認爲選項2是更多的Rails方式。 – sameera207

+0

@ Sameera207你介意解釋爲什麼?:) – Coderama

1

如果想將其移動到一個幫手,你應該把它到client_helper移動,因爲它的東西只爲你的客戶端模型,而不是爲整個應用程序。

你說的方法雖然是靜態類方法還是實例方法?如果它是一個實例方法,那麼你的模型(即使它們在視圖中)可以調用該方法。如果它是一個靜態類方法,那麼你的視圖也可以像使用其他靜態類方法一樣調用它(即Client.do_method或其他)。

我不明白爲什麼它需要在助手中,除非你的方法絕對與你的模型無關,在這種情況下,這將是一個不同的問題。

+0

我能想到很多絕對不應該出現在模型中的代碼。思考:有助於確定呈現選項的代碼(可以在控制器或視圖中調用),檢查訪問權限的代碼,與不具有清晰所有權關係的多個模型交集的代碼(例如,可與「游泳者「和」跑步者「,但不是'Cycler')等等等等 – Domi

1

在模型和輔助5共同方法:

選項1:

    You can just define the method inside the Client class, and then call it from the view, 
        like this: 

        class Client < ActiveRecord::Base 
         def self.my_class_method 
         end 
         def my_instance_method 
         end 
        end 

        And then in your view: 

         <%= Client.my_class_method %> 
         <%= @client.my_instance_method %> 

選項2:

    module MyModule 
         def my_method 
         end 
        end 

        Include the module in ApplicationHelper so it is available to all your views: 

        module ApplicationHelper 
         include MyModule 
        end 

        Then in your view you can call it easily: 

        <%= my_method %> 

選項3:

    module MyModule 
         def my_method(amount) 
         end 
        end 

        In your model: 

        class Client < ActiveRecord::Base 
         include MyModule 
         def self.other_method(amount) 
          my_method(amount) 
         end 
        end  

        Then in your view you can call it easily: 

        <%= Client.other_method(amount) %> 

方案4: 或者你可以聲明helper方法作爲一類功能,並使用它,像這樣:

    module Myhelper 
         def self.my_village 
          "bikharniyan kallan,Rajasthan,India" 
         end 
        end 

        Then in your view you can call it easily: 

        <%= Myhelper.my_village %> 

選項5: 使用了許多幫手控制器 幫手=>

     module Myhelper 
          def my_info 
           "my information is" 
          end 
         end 

         module Myhelper1 
          def my_village 
           "bikharniyan kallan,Rajasthan,India" 
          end 
         end 

        ApplicationHelper=> 

         module ApplicationHelper 
          include Myhelper 
          include Myhelper1 
         end 

        ApplicationController 

         class ApplicationController < ActionController::Base 
          include ApplicationHelper 
         end 

        YourController 

         class YourController < ActionController::Base 
          def action 
           my_info 
           my_village 
          end 
         end 
+7

如果你打算直接複製我的答案的一部分,有禮貌地說它來自我。 –

+1

我只是結合所有方法形式不同的來源爲用戶提供幫助 – Mukesh

0

此時,護欄5,你可以簡單地把你常用的方法爲application_record.rb

class ApplicationRecord < ActiveRecord::Base 
    self.abstract_class = true 

    def self.common_class_method 
    # some awesome implement 
    end 

    def common_method 
    # implement this 
    end 
end 

然後在每個模型類,你可以通過撥打common_class_methodYourClassName.common_class_method

或調用由common_methodYourClassInstance.common_method