Suppress closure return value in ruby on rails erb file
I have a simple erb template that pulls information from a (Grit) git repository. It displays the information just fine, but it also displays the return value of the closure which is the array of commits I'm iterating through. I found a similar question here, but the solution does not change my output at all.
#controller method for home
def home
@repo = Repo.new("/home/matt/gitrepo")
end
#home.html.erb
<%= @repo.commits[1..3].each do |commit| %>
<%= content_tag(:div, "#{commit.author}", :class=>"author") %>
<% end %>
the page output is
Matt Phillips
Matt Phillips
Matt Phillips
[#<Grit::Commit "e761477be972855b0c4273c1c7837baa25178210">, #<Grit::Commit "18开发者_开发知识库140c17632fae7dbf33cdd5e372f96ebe8104de">, #<Grit::Commit "f7e8ee090bb3e8259627179287d1722c328b614f">]
How can I suppress the array?
EDIT: Here's what it looks like in irb
ruby-1.9.2-p180 :011 > include Grit
=> Object
ruby-1.9.2-p180 :012 > @repo = Repo.new("/home/matt/gitrepo")
=> #<Grit::Repo "/home/matt/gitrepo/.git">
ruby-1.9.2-p180 :013 > @repo.commits[1..3].each do |commit|
ruby-1.9.2-p180 :014 > puts commit.author
ruby-1.9.2-p180 :015?> end
Matt Phillips
Owen Johnson
Matt Phillips
=> [#<Grit::Commit "e761477be972855b0c4273c1c7837baa25178210">, #<Grit::Commit
"18140c17632fae7dbf33cdd5e372f96ebe8104de">, #<Grit::Commit
"f7e8ee090bb3e8259627179287d1722c328b614f">]
ruby-1.9.2-p180 :016 >
Change this
<%= @repo.commits[1..3].each do |commit| %>
to this
<%- @repo.commits[1..3].each do |commit| %>
had a ;nil at the end before I realized the = was there
<%= and <%- both indicate we output the results of the contained Ruby expression. In the case of your each block the result is the array.
Change to:
<% @repo.commits[1..3].each... %>
That tells us to not output anything
精彩评论