Question

HTML output generated code is wrong.

I have the following Post model

class Post < ActiveRecord::Base
  belongs_to :user
  attr_accessible :title, :description, :price, :status, :pageviews, :attachments_attributes
  has_many :attachments, :as => :attachable
  accepts_nested_attributes_for :attachments

  validates :user_id, presence: true
  validates :title, presence: true, length: { maximum: 50 }
  validates :price, presence: true
  validates :description, presence: true, length: { maximum: 1000 }

  default_scope order: 'posts.created_at DESC'
end

my new.html.erb

<% provide(:title, "Novo anúncio")%>
<h1>Novo anúncio</h1>
<%= simple_nested_form_for @post, :html => { :multipart => true } do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class='field'>
    <%= f.label "Título" %><br />
    <%= f.text_field :title, size: 60, maxlength: 50 %>
  </div>
  <div class='field'>
    <%= f.label "Descrição" %><br />
    <%= f.text_area :description, cols: 100 %>
  </div>
  <div class='field'>
    <%= f.label "Valor" %><br />
    <%= f.text_field :price %>
  </div>
  <%= f.fields_for :attachments do |attachment_form| %>
    <p><%= image_tag(attachment_form.object.file.url) if attachment_form.object.file? %></p>
    <div class='field'>
      <%= attachment_form.label :description %><br />
      <%= attachment_form.text_area :description %>
    </div>
    <div class='field'>
      <%= attachment_form.label :file %><br />
      <%= attachment_form.file_field :file %>
    </div
    <%= attachment_form.link_to_remove "Remove this attachment" %>  
  <% end %>
  <%= f.link_to_add "Add attachment", :attachments %>

  <div class='actions'>
    <%= f.submit "Enviar" %>
  </div>
<% end %>

the code above is generating the following html:

<form accept-charset="UTF-8" action="/posts" class="simple_form new_post" enctype="multipart/form-data" id="new_post" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="7xO5QsSObfIO4A+FoKxCpFiqA5k3noIQBrpdwh2Iq2g=" /></div>

  <div class='field'>
    <label class="string optional control-label" for="post_Título"> Título</label><br />
    <input id="post_title" maxlength="50" name="post[title]" size="60" type="text" />
  </div>
  <div class='field'>
    <label class="string optional control-label" for="post_Descrição"> Descrição</label><br />
    <textarea cols="100" id="post_description" name="post[description]" rows="20"></textarea>
  </div>
  <div class='field'>
    <label class="string optional control-label" for="post_Valor"> Valor</label><br />
    <input id="post_price" name="post[price]" size="30" type="text" />
  </div>
    <a href="javascript:void(0)" class="add_nested_fields" data-association="attachments">Add attachment</a>

  <div class='actions'>
    <input name="commit" type="submit" value="Enviar" />
  </div>
</form><div id="attachments_fields_blueprint" style="display: none"><div class="fields">
    <p></p>
    <div class='field'>
      <label class="text optional control-label" for="post_attachments_attributes_new_attachments_description"> Description</label><br />
      <textarea cols="40" id="post_attachments_attributes_new_attachments_description" name="post[attachments_attributes][new_attachments][description]" rows="20"></textarea>
    </div>
    <div class='field'>
      <label class="file optional control-label" for="post_attachments_attributes_new_attachments_file"> File</label><br />
      <input id="post_attachments_attributes_new_attachments_file" name="post[attachments_attributes][new_attachments][file]" type="file" />
    </div
    <input id="post_attachments_attributes_new_attachments__destroy" name="post[attachments_attributes][new_attachments][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields">Remove this attachment</a>  
</div></div>

As you can see, the </form> is being called BEFORE the nested_attributes fields.

I have no clue what it's happening here.

Appreciate any help.

Was it helpful?

Solution 2

The error was in the penultimate <div> tag that I missed to close.

OTHER TIPS

Can't tell for sure since you didn't post this part of your code, but in your controller's "new" action, make sure that you are

@post = Post.new
@post.attachments.build

The second line is important to setup a placefolder for the form to use.

Source: Problems with nested form fields showing up

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top