Reputation: 1771
This is really simple, but i'm going slightly mad and probably missing something that's staring me in the face. Can anyone help?
Basically, I have a simple each loop that's returning an extra rogue line. Even when there's nothing in the db, I get one line returned!
My show view including the loop is:
<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 %>
The controller code is:
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
Upvotes: 0
Views: 336
Reputation: 84114
Your call to form_for
looks like this
form_for([@mailer,@mailer.mailer_products.build]) do |f|
You get an extra blank item because that's what calling .build
on mailer_products
does: it appends a new instance to the array
When the form is after the loop this doesn't matter, but when things are the other way around the loop will be on the modified array
Upvotes: 2
Reputation: 5087
My usual mistake is adding a <%=
instead of a <%
on the loop...
<%= @foo.each do |itme| %>
# do stuff
<% end %>
which should be
<% @foo.each do |itme| %>
# do stuff
<% end %>
Double check your tags...
Upvotes: 0