简体   繁体   English

使用 yard 的 Rails 控制器文档

[英]Documentation of Rails controllers using yard

When trying to create the documentation of my rails controller and the used parameters I'm getting stuck because yard seems to expect, that the parameters exists as method parameters.在尝试创建我的 rails 控制器和使用的参数的文档时,我被卡住了,因为 yard 似乎期望参数作为方法参数存在。

# Renders a list of items
# @param params [Hash] the search parameters
# @option params [String] 'info' Only return items with this specific info
# @option params [Integer] 'limit' Only return <limit> items
def index
  # ... do something smart here
end

So for this documentation yard will raise a warning and doesn't create the documentation:所以对于这个文档 yard 会发出警告并且不会创建文档:

[warn]: @param tag has unknown parameter name: params 
in file `app/controllers/items_controller.rb' near line 8

Is there a way to document these kinds of items using yard or will I need to do this manually?有没有办法使用 yard 来记录这些类型的物品,还是我需要手动完成?

I am not aware of a Rails convention to document action parameters through Yard.我不知道通过 Yard 记录操作参数的 Rails 约定。 Will be more than happy to learn about one if anyone out there knows more.如果有人知道更多,将非常乐意了解一个。

Yard is very flexible.院子非常灵活。 You can for example define your own tags for the project.例如,您可以为项目定义自己的标签 Add a tag dedicated to document your controller action parameters.添加专门用于记录控制器操作参数的标签。

First include the tag definition in the Yard options file .yardopts .首先在 Yard 选项文件.yardopts包含标签定义。 Th file is picked up from the project's root directory: Th 文件是从项目的根目录中提取的:

--type-name-tag controller_action_param:"Controller Action Parameters"

Use the new tag in your controller actions like in the example below:在您的控制器操作中使用新标签,如下例所示:

# Renders a list of items
# @controller_action_param :info [String] Only return items with this specific info
# @controller_action_param :limit [Integer] Only return <limit> items
def index
  # ... do something smart here
end

The convention you now defined is that the parameter name is the key used to access the params data - params[:info] for example.您现在定义的约定是参数名称是用于访问 params 数据的键 - 例如 params[:info]。

Define a tag name and heading text that suits you most - this is just an example.定义最适合您的标签名称和标题文本 - 这只是一个示例。

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

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