0

需要一些建议,我知道直接在视图中分配变量是一种不好的做法。但我发现只有一种方法可以实现引导程序'nav-tab'。可能有其他方法吗?谢谢

<div class="tabbable tabs-left">
  <ul class="nav nav-tabs">
    <% answer = [] %>  // Here it is..
    <% @questions.each_with_index do |question, index| %>
        <li class="<%= "active" if index == 0 %>">
          <a href="#pane<%= index + 1 %>" data-toggle="tab">
            <%= question.title %>
          </a>
        </li>
       <% answer << [index, question]%>
    <% end %>
  </ul>
  <div class="tab-content">
    <% answer.each do |i, a| %>
      <div id="pane<%= i + 1 %>" class="tab-pane <%= 'active' if i == 0 %>">
        <div class="links">
          <%= link_to '', edit_question_path(a), class: "icon-pencil" %>
          <%= link_to '', a, method: :delete, data: { confirm: 'Are you sure?' }, class: "icon-remove" %>
        </div>
        <%= raw a.content %>
      </div>
    <% end %>
  </div><!-- /.tab-content -->
</div><!-- /.tabbable -->
4

1 回答 1

2

您可以使用以下方法构建answer控制器:

@answer = []
@questions.each_with_index do |question, index|
  @answer << [index, question]
end

然后@answer在该<div class="tab-content">部分中使用,而不是answer.

或者您可以在控制器中构建整个<ul class="nav nav-tabs">部分,因为您已经在循环@questions构建@answer

@answer = []
@all_questions = ""
@questions.each_with_index do |question, index|
  @all_questions << "<li class=\"#{"active" if index == 0}\"><a href=\"#pane#{index + 1}\" data-toggle=\"tab\">#{question.name}</a></li>"
  @answer << [index, question]
end

然后<ul class="nav nav-tabs">将视图中的部分更改为:

<ul class="nav nav-tabs">
  <%= @all_questions %>
</ul>
于 2013-07-12T22:32:35.103 回答