Search code examples
ruby-on-railsformsnested-formsformtastic

How do I generate multiple inputs in formtastic?


Adapted from the nested form Railscast, I have:

In my model

class Post < ActiveRecord::Base
  has_many :fields
  accepts_nested_attributes_for :fields
end

class Field < ActiveRecord::Base
  belongs_to :post
end

In my controller

def new
  @post = Post.new
  4.times { @post.fields.build }

  respond_to do |format|
    format.html
  end
end

In my view

<%= semantic_form_for @post do |f| %>

  <%= f.inputs do %>
    <%= f.input :title %>
  <% end %>

  <%= semantic_fields_for :fields do |h| %>
    <%= h.input :name %>
  <% end %>

  <%= f.buttons do %>
    <%= f.commit_button %>
  <% end %>
<% end %>

The problem is that this only generates one :field input even though i ran @post.fields.build four times. I can't figure out how to generate multiple inputs so the user can enter multiple fields.

Sorry if this is obvious but I'm new to Rails and pretty new to programming overall.


Solution

  • Your nested form ain't correct
    Change <%= semantic_fields_for :fields do |h| %> to
    <%= f.inputs :for => :fields do |h|%>