模型和助手中的常用方法

时间:2011-07-31 03:12:28

标签: ruby-on-rails design-patterns

我的模型中存在一个常见的方法,因为它是由我的模型调用的。回顾一下,我的观点也需要这种模型方法。为了实现这一目标,我有:

  1. 将模型方法移至application_helper.rb文件
  2. 我的模型通过在ActiveRecord模型顶部添加include ApplicationHelper来调用application_helper方法
  3. 功能明智,它有效。但这是一种好的做法吗?

    我的模型看起来像这样:

    class Client < ActiveRecord::Base
      include ApplicationHelper
    end
    

4 个答案:

答案 0 :(得分: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 %>

答案 1 :(得分:1)

如果您 想要将其移动到帮助程序,则应将其移至client_helper,因为它仅适用于您的客户端模型,而不适用于整个应用程序。

您说的方法是静态类方法还是实例方法?如果它是一个实例方法,那么您的模型(即使它们在视图中)也可以调用该方法。如果它是一个静态类方法,那么你的视图也可以像使用任何其他静态类方法一样调用它(即Client.do_method或其他东西)。

我认为没有任何理由需要帮助你,除非你的方法绝对与你的模型无关,在这种情况下这将是一个不同的问题。

答案 2 :(得分: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:                         或者您可以将辅助方法声明为类函数,并像这样使用它:

                    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:                       在控制器中使用许多帮助器                         助手=&GT;

                        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

答案 3 :(得分:1)

此时,使用rails 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

相关问题