Partial Document Options in Rails

Is there any standard or new standard for documenting parameters that can be passed to the Rails part?

When _my_partial.html.erb expects the local variable title and elements passed with render 'my_partial', title: t, elements: e , should be the usual way to document their names, expected types and roles without reading all the partial code. Something like RDoc or Tomdoc for methods and classes. Does not exist?

Edit: I found a message whose author favors parameter initialization using <% var ||= 'default_val' %> in the first lines of a partial, which is really safe practice and a kind of dock code. Is there really no comment / declaration parameter for this?

+4
source share
1 answer

At the beginning of your partial, just call all the referenced variables.

 # _my_partial.html.erb <% title %> <--- first line of file <% elements[0] %> <h3><%= title %></h3> <% elements.each do |element| %> <p> etc ... </p> 

Reasons why this is good for your project:

  • it does not rely on comments or files without code.
  • any project developer can quickly find out which variables are needed by looking at the top of the file in question.
  • By invoking variables, you guarantee that the missing variable will throw an exception. Items
  • are called with square brackets because we also want it to explode if it doesn't list, right?

The practice of using <% var ||= 'default_val' %> is actually unsafe because it allows you to hide errors. You want your code to explode immediately when something has not been done correctly. And if these variables must be passed, then you want the code to explode when they are not there.

+3
source

Source: https://habr.com/ru/post/1439799/


All Articles