Ruby-on-rails 模型和助手中的常用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6887319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:41:06  来源:igfitidea点击:

Common method in model and helper

ruby-on-railsdesign-patterns

提问by Coderama

I have a common method that exists in my model because it is called by my model. Retrospectively, my view also requires this model method. In order to accomplish this, I have:

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

  1. moved the model method to the application_helper.rbfile
  2. my model calls the application_helper method by adding include ApplicationHelperat the top of my ActiveRecord model
  1. 将模型方法移至application_helper.rb文件
  2. 我的模型通过include ApplicationHelper在我的 ActiveRecord 模型的顶部添加来调用 application_helper 方法

Functionality wise, it works. But is this good practice?

功能明智,它有效。但这是好的做法吗?

My Model looks like this:

我的模型看起来像这样:

class Client < ActiveRecord::Base
  include ApplicationHelper
end

回答by David Grayson

Writing include ApplicationHelperin to your model is bad practice because ApplicationHelper is a nice place to put tons of helper functions you need in your views. These functions will end up being imported as instance methods of your model. These functions are mostly unrelated to your model and will not work if they depend on things like paramsor request. Here are two other options:

写入include ApplicationHelper你的模型是不好的做法,因为 ApplicationHelper 是一个很好的地方,可以在你的视图中放置大量你需要的辅助函数。这些函数最终将作为模型的实例方法导入。这些函数大多与您的模型无关,如果它们依赖于params或 之类的东西,它们将无法工作request。这是另外两个选项:

Option 1:

选项1:

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

您可以在Client类中定义方法,然后从视图中调用它,如下所示:

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 %>

Option 2:

选项 2:

Make a separate module in liband include it in the places you need it. The file name should match the module name for auto-loading to work.

在其中创建一个单独的模块lib并将其包含在您需要的地方。文件名应与模块名称匹配,以便自动加载工作。

In lib/my_module.rb:

lib/my_module.rb

module MyModule
    def my_method
    end
end

In your model:

在您的模型中:

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

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

将模块包含在 ApplicationHelper 中,以便您的所有视图都可以使用它:

module ApplicationHelper
    include MyModule
end

Then in your view you can call it easily:

然后在您的视图中,您可以轻松调用它:

<%= my_method %>

回答by MrDanA

If you dowant to move it to a helper, you should move it in to the client_helper, as it is something just for your Client model and not for the whole application.

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

The method you speak of though, is it a static class method or an instance method? If it's an instance method, then your models (even if they're in views) can call that method. If it's a static class method, then your views can use it too by calling it like any other static class method (i.e, Client.do_methodor something).

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

I don't see any reason why it needs to be in a helper, unless your method has absoloutely nothing to do with your model, in which case that would be a different question.

我看不出有任何理由需要它在助手中,除非您的方法与您的模型完全无关,在这种情况下,这将是一个不同的问题。

回答by Mukesh

5 Common method in model and helper:

5 模型和助手中的常用方法:

Option 1:

选项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 %>

Option 2:

选项 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 %>

option 3:

选项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) %>  

Option 4:Or you can declare the helper method as a class function, and use it like so:

选项 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 %>  

option 5:use many helper in controller helper=>

选项 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

回答by Nhat Dinh

At this moment, with rails 5 you can simply push your common method into application_record.rb

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

Then in each model class you can call common_class_methodby : YourClassName.common_class_method

然后在每个模型类中,您可以common_class_method通过以下方式调用:YourClassName.common_class_method

or call common_methodby: YourClassInstance.common_method

或致电common_methodYourClassInstance.common_method