0

I'm want to use the ryanb/nested_form gem (or similar) to create associations with many different field types. Consider a model called content which might have many content_blocks that should have text inputs, text areas, or image upload fields (different field types) dependant on what is required. Anybody got an ideas on how to achieve this? So far I can only work out how to add multiple fields of the same type. i.e the standard project -> task -> subtask ... pattern.

Or is there a better solution?

Thanks! -J

  • You mean you want to write a form for content with nested form for content_block. And you should able to add multiple content_blocks while creating content. Is it correct? – Anuja May 29 '15 at 12:22
  • Yes that is it, the key being different form fields in the content_blocks something like [link](http://i.imgur.com/SbIu6Bv.png) – Grimmrobe May 29 '15 at 13:16

2 Answers2

0

You can use cocoon gem for nested_form. Also, there is a good tutorial on nested form by Ryan Bates on Railscasts

Emu
  • 5,337
  • 3
  • 26
  • 49
0

If your models are defined correctly, then the params received by below code should contain all the information for creating multiple tasks and sub-tasks in one step.

<%= f.fields_for :tasks do |task_form| %>
  <%= task_form.text_field :name %>

  <%= task_form.fields_for :sub_tasks do |sub_task_form| %>
    <%= sub_task_form.text_field :sub_name %>
    <%= sub_task_form.link_to_remove "Remove this subtask" %>
  <% end %>
  <p><%= f.link_to_add "Add a task", :sub_tasks %></p>

  <%= task_form.link_to_remove "Remove this task" %>
<% end %>
<p><%= f.link_to_add "Add a task", :tasks %></p>

Please notice that the latest version of nested_form do generate correctly the ids for multiple creation on the same step. For having this issue fixed you have to use:

gem 'nested_form', github: 'ryanb/nested_form'
mmsilviu
  • 771
  • 7
  • 19