Rails fields_for with ajax

I am creating a dynamic form constructor. And I have a problem that I cannot fix.

So, I have a db table called "form" forms can have "fields".

The problem is that when the user creates a new field (click add-field), he must ajax a new field for .. this field.

The problem is that I cannot just do something like this:
<%= Form.fields_for Field.new do |field| %>
  <%= field.text_field :name%>
<% end %>

Does anyone have any ideas? Yes, I look at railscasts, yes, I googled, yes, I found a repository of "complex forms" on github.

But no luck (for now)

+3
source share
1 answer

javascript- ( , ), .

,

class Form < ActiveRecord::Base
  has_many :fields
  accepts_nested_attributes_for :fields

HTML , -

<label for="form_fields_attributes_0_name">
<input id="form_fields_attributes_0_name" name="form[fields_attributes][0][name]" type="text" />

javascript,

<label for="form_fields_attributes_1_name">
<input id="form_fields_attributes_1_name" name="form[fields_attributes][1][name" type="text" />

-

$('#form_fields_attributes_1_name').attr('id').split('_');

$('#form_fields_attributes_1_name').attr('name').split(/\]\[/);

.

, .

+6

Source: https://habr.com/ru/post/1771723/


All Articles