简体   繁体   English

Rails控制器/模型方法

[英]Rails Controller/Model methods

I am watching Code School Rails testing course. 我正在观看Code School Rails测试课程。 There is an instance of the class zombie . 有一个zombie类的实例。 The zombie model has a method: zombie模型有一个方法:

def avatar_url
  ...
end

Within the test, .rb file has the following: 在测试中, .rb文件具有以下内容:

z.avatar_url
  1. When I call a method like this, how does Rails distinguish if I'm calling a controller or model method? 当我调用这样的方法时,如果我调用控制器或模型方法,Rails如何区分? I hadn't thought of calling a model method from other than a controller, and only like Model.method and not object.method . 我没有想过从控制器以外调用模型方法,只有Model.method而不是object.method
  2. If both my controller and my model have a method with the same name, how would Rails know which one to call? 如果我的控制器和我的模型都有一个同名的方法,那么Rails如何知道要调用哪一个?

Update: 更新:

Lets take the class String as example, it is not a model, right? 让我们以String类为例,它不是模型,对吧?

So I could say: 所以我可以说:

s = String.new
s.capitalize

If this call doesn't go to a model and not to a controller, where does it go then? 如果此调用没有转到模型而不是控制器,那么它在哪里? Where would a class like String be defined in the Rails directory? 在Rails目录中定义类似String的类在哪里?

A method inside a controller can only be called via URL. 控制器内部的方法只能通过URL调用。
Example: 例:

/things/super_action

Should call the def super_action inside ThingsController . 应该在ThingsController调用def super_action ThingsController

As for Model methods, they can be accessed anywhere. 至于模型方法,它们可以在任何地方访问。 Just note if they are instance or class methods: 请注意它们是实例还是类方法:

Model.ultra_method

This is a class method call, it is probably defined as def self.ultra_method . 这是一个类方法调用,它可能被定义为def self.ultra_method

m = Model.new
m.instance_method

This is a instance method call, and it is probably defined as def ultra_method . 这是一个实例方法调用,它可能定义为def ultra_method

UPDATE UPDATE

String is a core class of ruby language. Stringruby语言的core类。 As is Array , Number , etc. In your example you are creating an instance of String and calling an instance method of the String class. 正如ArrayNumber等,在你的榜样,你正在创建一个instanceString并调用一个instance method中的String类。

It seems like you're new to Ruby as well as Rails. 看起来你是Ruby以及Rails的新手。 In Ruby, a class is sort of like a description of a type of object (although the class itself is an object, too). 在Ruby中,类有点像对象类型的描述(尽管类本身也是一个对象)。 Whenever there is a class defined, you can create new instances of it, as with String.new . 只要定义了类,就可以像String.new一样创建它的新实例。 Note that classes always have capitalized names. 请注意,类始终具有大写的名称。

Class methods are methods that work on the class itself. 类方法是对类本身起作用的方法。 You can tell when a method is a class method because it will be attached to the capitalized name of the class (just like String.new ). 您可以判断方法何时是类方法,因为它将附加到类的大写名称(就像String.new一样)。 On the other hand, instance methods only work on an instance of the class, not on the class itself (eg str = String.new; str.capitalize! ). 另一方面,实例方法仅适用于类的实例,而不适用于类本身(例如str = String.new; str.capitalize! )。 Usually there are more instance methods than class methods, because instances are the things that you're actually working with ( new is the most common class method you'll see). 通常有更多的实例方法而不是类方法,因为实例是你实际使用的东西( new是你将看到的最常见的类方法)。

As others have mentioned here, String is not a Rails model; 正如其他人在这里提到的,String不是Rails模型; it's a basic Ruby class. 这是一个基本的Ruby类。 When you're working in Rails, you have access to all the regular Ruby classes as well as other classes and methods that are defined within Rails' source code. 当您在Rails中工作时,您可以访问所有常规Ruby类以及Rails源代码中定义的其他类和方法。 So String is not defined in Rails itself, but Rails does provide some useful instance methods for strings (eg str.to_date ). 所以String没有在Rails本身中定义,但是Rails确实为字符串提供了一些有用的实例方法(例如str.to_date )。

A model in Rails is really just a Ruby class. Rails中的模型实际上只是一个Ruby类。 To understand the workings of a model, you should make sure you understand how Ruby classes work. 要了解模型的工作原理,您应该确保了解Ruby类的工作原理。 What makes Rails models special is that they inherit from a class defined in Rails' source code known as ActiveRecord (any class in Ruby can inherit from another class, this is just one example of that). Rails模型的特殊之处在于它们继承自Rails的源代码中定义的类,称为ActiveRecord(Ruby中的任何类都可以从另一个类继承,这只是其中的一个例子)。 ActiveRecord has a number of class and instance methods, which are also available to your models because they inherit from ActiveRecord. ActiveRecord有许多类和实例方法,它们也可用于模型,因为它们继承自ActiveRecord。 For example, if you have a class (model) called Person, you can automatically use the Person.find(id) class method to look up a particular instance of the Person class in the database. 例如,如果您有一个名为Person的类(模型),则可以自动使用Person.find(id)类方法在数据库中查找Person类的特定实例。 You also have the person.save instance method to save the instance to the database. 您还有person.save实例方法将实例保存到数据库。

All of this was confusing to me when I first started, so my best advice is to familiarize yourself with Ruby as you learn Rails. 当我刚开始时,所有这一切都让我感到困惑,所以我最好的建议是在学习Rails时熟悉Ruby。

You can call Model class/instance methods from anywhere in Rails. 您可以在Rails中的任何位置调用Model类/实例方法。 Model are just the mapping to your database and acts as a proxy for your database. 模型只是数据库的映射,并充当数据库的代理。 When you are calling 当你打电话的时候

z.avatar_url

you are calling a method on "z" model instance. 你正在调用“z”模型实例上的方法。 So not matter from where you call it will always call the model's method. 所以无论你在哪里调用它总是会调用模型的方法。

If you define a method with same name in both controller and model, you would always be calling a model's method with model instance or model class. 如果在控制器和模型中定义具有相同名称的方法,则始终使用模型实例或模型类调用模型的方法。 Controller methods are simply action in Rails they are never referred directly from anywhere. 控制器方法只是Rails中的操作,它们永远不会从任何地方直接引用。 They are used for Rails routing. 它们用于Rails路由。

Hope I am clear. 希望我很清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM