带有HTML标记的Helper Ruby

时间:2016-06-28 09:03:41

标签: ruby-on-rails ruby middleman

我正在尝试编写一个帮助程序来创建一个可折叠的Grop。 我转换了

<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
  <h4 class="panel-title">
    <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
      Collapsible Group Item #1
    </a>
  </h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
  <div class="panel-body">
     text to answer
  </div>
</div>

  def faqbox(question, answer, number)
open :div, :class => "panel panel-default" do
  open :div, :class => "panel-heading", :role => "tab", :id => "heading#{number}" do
    open :h4, :class => "panel-title" do
      open :a, :role => "button", :data-toggle => "collapse", :data-parent => "#accordion", :href => "#collapseOne", :aria-expanded => "true", :aria-controls => "collapse#{number}" do
        "#{question}"
      end
    end
  end
end
open :div, :id => "collapse#{number}", :class => "panel-collapse collapse in", :role => "tabpanel", :aria-labelledby => "heading#{number}" do
  open :div, :class => "panel-body" do
    "#{answer}"
  end
end 
end

但它给我“TypeError at / getting-help 没有将符号隐式转换为字符串“

我和中间人一起使用红宝石。我该怎么办?

1 个答案:

答案 0 :(得分:0)

请考虑使用partial而不是大量的嵌套open

# _faqbox.html.erb

<% heading = "heading#{number}"%>
<% collapse ="collapse#{number}" %>

<div class="panel panel-default">
  <div class="panel-heading" role="tab" id=<%= heading %>>
    <h4 class="panel-title">
      <a role="button" data-toggle="collapse" data-parent="#accordion" href="#<%= collapse %>" aria-expanded="true" aria-controls="<%= collapse %>">
        <%= question %>
      </a>
    </h4>
  </div>

  <div id="<%= collapse %>" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
    <div class="panel-body">
       <%= answer %>
    </div>
  </div>
</div>


# some view
<%= render partial: "faqbox", locals: {question: "foo", answer: "bar", number: 3} %>

结果

partial result