模型和助手中的常用方法

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

  1. 将模型方法移动到application_helper.rb文件
  2. 我的模型通过在ActiveRecord模型的顶部添加include ApplicationHelper调用application_helper方法

function明智,它的工作原理。 但这是一个好习惯吗?

我的模型看起来像这样:

 class Client < ActiveRecord::Base include ApplicationHelper end 

在您的模型中编写include ApplicationHelper是不好的做法,因为ApplicationHelper是一个在视图中放置大量辅助函数的好地方。 这些函数最终将作为模型的实例方法导入。 这些function大多与您的模型无关,如果它们依赖于诸如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 %> 

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

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

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

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:在controller helper =>中使用许多帮助器

  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 

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