这些奇怪的角色是什么意思?

时间:2022-08-03 00:12:58

I'm reading a Ruby book but it doesn't explain the following:

我正在读一本Ruby的书,但它没有解释以下内容:

  1. What is this: validates :name, :presence => true

    什么是:validates:name:presence => true

    • I mean I know what it does but what's validates? Is it a method of the validator class? If so, how come it's called without mentioning the class name first?

      我的意思是我知道它的作用但是什么是有效的?它是validator类的一个方法吗?如果是,为什么不先提到类名就调用它呢?

    • What's the meaning of : in the previous code and in Rails on general?

      在以前的代码和Rails中,一般来说是什么意思?

  2. In the following code: <%= form_for([@post, @post.comments.build]) do |f| %>

    在以下代码中:<%= form_for([@post, @post.comments.build]) |f| %>。

    • Is form_for an object or a procedural function?

      form_for是对象还是过程函数?

    • What's the meaning of the | character in |f|

      |f|中的|字符是什么意思

  3. In <%= link_to 'Edit Post', edit_post_path(@post) %>

    在<%= link_to 'Edit Post'中,edit_post_path(@post) %>

    • Who, where and when was edit_post_path method defined?

      edit_post_path方法定义了谁、在何处以及何时?

    • To which class it belongs?

      它属于哪个类别?

1 个解决方案

#1


6  

  1. validates is a method, part of the validators in Rails. It is declared in (actually, included to) a superclass, that is why it does not have to be declared in the model. The : in front of anything signifies a symbol, not a variable. Symbols are part of Ruby, somewhat similar to strings.
  2. 验证是一个方法,是Rails中的验证器的一部分。它被声明在超类中(实际上包括在超类中),这就是为什么它不必在模型中声明的原因。在任何东西前面都表示符号,而不是变量。符号是Ruby的一部分,有点类似于字符串。
  3. form_for is a method, which takes a number of parameters and a block (that is why there is a do afterwards). The | is part of Ruby syntax, the way you enclose code block parameters.
  4. form_for是一个方法,它接受许多参数和一个块(这就是为什么后面会有一个do)。|是Ruby语法的一部分,是封装代码块参数的方式。
  5. edit_post_path is defined by the Rails magic and the routes. It is a helper method.
  6. edit_post_path由Rails魔法和路由定义。它是一个辅助方法。

I encourage you to read this book about Ruby to get more familiar with symbols, code blocks, modules and other things that make Ruby a great programming language.

我鼓励您阅读这本关于Ruby的书,以便更熟悉符号、代码块、模块和其他使Ruby成为优秀编程语言的东西。

#1


6  

  1. validates is a method, part of the validators in Rails. It is declared in (actually, included to) a superclass, that is why it does not have to be declared in the model. The : in front of anything signifies a symbol, not a variable. Symbols are part of Ruby, somewhat similar to strings.
  2. 验证是一个方法,是Rails中的验证器的一部分。它被声明在超类中(实际上包括在超类中),这就是为什么它不必在模型中声明的原因。在任何东西前面都表示符号,而不是变量。符号是Ruby的一部分,有点类似于字符串。
  3. form_for is a method, which takes a number of parameters and a block (that is why there is a do afterwards). The | is part of Ruby syntax, the way you enclose code block parameters.
  4. form_for是一个方法,它接受许多参数和一个块(这就是为什么后面会有一个do)。|是Ruby语法的一部分,是封装代码块参数的方式。
  5. edit_post_path is defined by the Rails magic and the routes. It is a helper method.
  6. edit_post_path由Rails魔法和路由定义。它是一个辅助方法。

I encourage you to read this book about Ruby to get more familiar with symbols, code blocks, modules and other things that make Ruby a great programming language.

我鼓励您阅读这本关于Ruby的书,以便更熟悉符号、代码块、模块和其他使Ruby成为优秀编程语言的东西。