2013-06-04 32 views
2

的零數據我對軌道上的ruby和在rails 3和mac osx8中構建基本應用程序相對較新。不能刪除不在表

我正在經歷一個額外的空白數據在每次循環迭代的視圖設置,在這裏:

<% @exercise.instruction_types.each do |instruction_type| %> 
      <li><a href="#tab<%= instruction_type.id %>" data-toggle="tab"><%= instruction_order %>. <%= instruction_type.name %></a></li> 
      <% instruction_order = instruction_order + 1 %> 
<% end %> 

這是建立在演出一個額外的空白李項。當我加載該視圖時,我看到循環的額外迭代,結果字段的空值。即使數組爲[],也會發生這種情況。

Instruction_type型號:

class InstructionType < ActiveRecord::Base 
    has_many :instructions 
    has_many :exercises, :through => :instructions 
    attr_accessible :name 
end 

這裏是控制器:

def play 
    .. 
    @instruction_types = @exercise.instruction_types.build 
    .. 
end 

經過一番研究和以下建議從這裏Getting a rogue iteration from my .each loop 我在放在我view.html這個實例變量檢查pludded。 erb

<%= @exercise.instruction_types.inspect %> 

R endered查看輸出

[#<InstructionType id: 1, name: "Content", created_at: "2013-06-03 03:11:56", updated_at:  "2013-06-03 03:11:56">,... 
<InstructionType id: nil, name: nil, created_at: nil, updated_at: nil>} 

我想知道,零數據是如何創建的,我怎麼能阻止它在將來,我怎麼現在可以刪除它。

+0

'@ exercise.instruction_types.build'創建一個空的關聯實例,它還沒有被持久化到數據庫 – phoet

+0

感謝您的快速響應phoet。我也搜索瞭如何處理這個問題,但我還沒有弄明白,你能否進一步指導?我想以某種方式刪除它,堅持它到數據庫?我如何讓它停止出現在我的每個循環中? –

+0

我想我的後續行動是有另一種方式,我可以在我的練習視圖中訪問instruction_type數據,而不是控制器中的@ exercise.instruction_type.build命令? –

回答

1

如果您的play操作不應構建新的InstructionType(這是我從評論中收集的內容),請刪除.build調用。如果它,請確保在InstructionType上也撥打.save