ruby-on-rails Helper方法不呈现列表项

flvtvl50  于 11个月前  发布在  Ruby
关注(0)|答案(1)|浏览(118)

在Rails 7中,为什么下面的帮助器不呈现列表项?

def list(options)
  tag.ul(class: "a") do
    options.each do |option|
      tag.li(class: "b") do
        tag.span(option, class: "c")
      end
    end
  end
end

字符串
我调用<%= list(["X", "Y", "Z"]) %>,所呈现的只是<ul class="a"></ul>
我错过了什么?

nqwrtyyt

nqwrtyyt1#

通常情况下,使用ERB,你会这样写:

<%= tag.ul(class: "a") do %>
  <% ["X", "Y", "Z"].each do |option| %>
    <%= tag.li(class: "b") do %>         # <%= %> is the same as `concat`
      <%= tag.span(option, class: "c") %>
    <% end %>
  <% end %>
<% end %>

字符串
使用concat

def list(options)
  tag.ul(class: "a") do
    options.each do |option|
      concat(
        tag.li(class: "b") do
          # concat could be omitted here for a single tag
          concat tag.span(option, class: "c")
        end
      )
    end
  end
end


使用safe_join

def list(options)
  tag.ul(class: "a") do
    safe_join(
      options.map do |option|
        tag.li(class: "b") do
          tag.span(option, class: "c")
        end
      end
    )
  end
end

相关问题