简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM