2012-07-02 19 views
0

這真的很簡單,但我會有點生氣,可能會錯過一些讓我在臉上凝視的東西。誰能幫忙?Rails 3.1 - 每個循環的額外結果?

基本上,我有一個簡單的每個循環返回一個額外的流氓線。即使數據庫中沒有任何內容,我也會返回一行!

我的節目,包括循環的觀點是:

<p id="notice"><%= notice %></p> 

<p> 
    <b>Header:</b> 
    <%= @mailer.header %> 
</p> 

<p> 
    <b>Subtext:</b> 
    <%= @mailer.subtext %> 
</p> 

<div id="" class="" padding-left: 30px;>  
<h3>Mailer Products </h3> 

<ol id="mailer-Product-list"> 
<% @mailer.mailer_products.sort_by { |mailer_products| mailer_products.position }.each do |mailer_product| %> 
    <%= content_tag_for :li, mailer_product do %> 
    <%= mailer_product.product.cat_no %> 
<% end %> 
<% end %> 
</ol> 

    <%#= link_to 'Done', @product, :class => "standard-button" %> 
</div> 



<%= form_for([@mailer,@mailer.mailer_products.build]) do |f| %> 

    <div class="field"> 
    <%= f.label :product_id %><br /> 
    <%= f.text_field :product_id %> 
    </div> 

    <div class="field"> 
    <%= f.hidden_field :mailer_id, :value => @mailer.id %> 
    </div> 

    <div class="actions"> 
    <%= f.submit "Add Product" %> 
    </div> 
<% end %> 


<%= link_to 'Edit', edit_mailer_path(@mailer) %> | 
<%= link_to 'Back', mailers_path %> 

控制器代碼:

class MailersController < ApplicationController 

    def show 
    @mailer = Mailer.find(params[:id]) 
    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render :json => @mailer } 
    end 
    end 

class MailerProductsController < ApplicationController 

    def index 
    @mailer_products = MailerProduct.find(:all) 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render :json => @mailer_products } 
    end 
    end 
end 

end 
+0

當您在控制檯上輸入「mailer.mailer_products」(使用適當的郵件程序)時,您會得到什麼? – YuriAlbuquerque

+0

在你的'Mailer'模型中它是'has_many:mailer_products'嗎?如果是這樣,請嘗試回顯'@ mailer.mailer_products.count'。 –

+0

Pastin工作而不是工作版本的代碼是相當奇怪的。 –

回答

2

您對form_for調用如下

form_for([@mailer,@mailer.mailer_products.build]) do |f| 

,因爲這是呼籲mailer_products.build做你得到一個額外的空白項:其追加新實例到數組

當窗體在循環之後這並不重要,但是當事物以其他方式圍繞循環時將會在修改的數組上

0

我通常的錯誤是增加一個<%=而不是在循環中的<%的...

<%= @foo.each do |itme| %> 
# do stuff 
<% end %> 

這應該是

<% @foo.each do |itme| %> 
# do stuff 
<% end %> 

仔細檢查你的代碼...

+0

謝謝,標籤與上面完全一樣,所以這不是可悲的。 – Raoot