Hey all,
Let''s say we have this:
#view
= section "Contact" do
- field_list :class => "contacts-view" do |v|
= v.item "Contact Type", @contact.contact_type
#helper
def section(*args, &block)
label = String === args.first ? args.first : String ==args.second ?
args.second : nil
klass = "#{klass} #{type ? type.to_s.dasherize :
nil}".strip.presence
content = block ? capture(&block).html_safe : ""
return "" if content.blank?
content = content_tag(:legend, label) + content if label.present?
content_tag :fieldset, content, :class => klass, :id =>
options.delete(:id)
end
1) From what I read, passing a block into the rails capture method
will capture a block of html, so you can append/prepend other html to
it. But what does capture a block of html mean? Why wouldnt you be
able to append/prepend html to it otherwise?
2) I am not sure what the + operator is doing here:
content_tag(:legend, label) + content
Obviously, the content_for rails method wraps the string held in label
around legend tags. content local variable holds the html that was
created in the block passed into this iterator. But does the +
operator mean that we are prepending that html onto the legend tag
html?
thanks for response
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
My understanding of the + operator is as follows. The + operator works differently with arrays than it does with scalar values. With arrays, when taking two arrays as operands, it returns an array containing everything in the two oeprand arrays. In essence, + operator performs addition on scalar types and union on arrays. For string, it does string concatenation. But what it does with blocks of html, such as what is returned by content_tag, I would like to know. thanks for response -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/4Bz0OzmI56oJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
The docs say this about content_tag:
=Returns an HTML block tag of type name surrounding the content.
=
Ok, not too helpful unless you understand the subtleties of css/html
speak. But the docs provide some examples that should help clarify
things:
content_tag(:p, "Hello world!")
# => <p>Hello world!</p>
Presumably, the html is a String--the return value doesn''t look like a
number or a method.
As for how blocks work: a block is really just a function. You write a
block in your code immediately after calling a method, and the method
captures the block in a variable. Then at some point the method calls
the block. Here is an example:
def some_method(str, &func)
if block_given?
puts func.call(str)
else
puts str
end
end
some_method("John") do |name|
"Hello #{name}"
end
--output:--
Hello John
The block is this part:
do |name|
"Hello #{name}"
end
which can also be written as:
{ |name| "Hello #{name"}
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino wrote in post #1014812:> My understanding of the + operator is as follows. The + operator works > differently with arrays than it does with scalar values. With arrays, > when > taking two arrays as operands, it returns an array containing everything > in > the two oeprand arrays. In essence, + operator performs addition on > scalar > types and union on arrays. For string, it does string concatenation.Yes, that''s all correct. + is just a strange name for a ruby method. If you write: "hello" + " world" That is equivalent to: "hello".+("world") That may look confusing but suppose you were calling a method like split: "hello,world".split(",") That is the exact same format as the + method call: obj.meth_name(arg)> But > what it does with blocks of html >That''s not what the docs mean about the return value of content_tag. In html, the term ''block tag'' has a specific meaning. The docs aren''t describing the return value, which is actually a String. Good docs list the return type of a method because that is one of the most important things you can know about a method in addition to the argument types. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.