Understanding Rails concat Behavior

Asked 2 years ago, Updated 2 years ago, 62 views

Ruby on Rails 5 Application Program Some things I don't understand.

def list_tag(collection,prop)
  content_tag(:ul)do
    collection.each do | element |
      concat content_tag(:li,element.attributes[prop])
    end
  end
end

There is a description that says .
View helper, array of objects in the collection, and prop property name of the object.
View helper that prints the same properties of each object in list format.

The question is, I understand that within each, we use the concat to output the li element, but why doesn't the ul element need the concat?Please let me know.
(I am a beginner, so I would appreciate it if you could consider it.I am aware of the older version of Rails.)

ruby-on-rails

2022-09-30 21:42

1 Answers

I think this method has the following calling configuration:

 Blocking arguments for list_tag->content_tag->content_tag

list_tag must return a string; there is only one method call in list_tag that returns a string.Therefore, concat is not required.

Conversely, the block of the content_tag argument must combine the resulting string for each loop because the loop has multiple method calls.Therefore, concat must combine strings.

concat combines the string into the internal buffer of the instance variable (output_buffer).
content_tag combines the changed output_buffer in the argument block to make it its own return value.


2022-09-30 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.