2013-07-12 26 views
0

需要一些建議,我知道在視圖中直接分配變量是一種不好的做法。但我發現只有一種方式來實現bootstrap'nav-tab'。可能有另一種方式?由於在視圖中分配的Rails變量,需要建議

<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 --> 

回答

1

你可以在控制器建立answer,具有:

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

<div class="tab-content">部分然後用@answer,而不是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>