Javascript 如何在rails上添加动态表单并将结果保存到mongodb(mongoid)?
我是RubyonRails 5的新手,我正在尝试制作一个“调查应用程序”,使用户能够将问题作为动态表单动态添加到调查中。我设法让它运行,但添加问题按钮不添加问题表单,删除按钮不删除问题表单 填写字段并单击submit按钮后,它只保存我在survey model中指定的userJavascript 如何在rails上添加动态表单并将结果保存到mongodb(mongoid)?,javascript,ruby-on-rails,mongoid,Javascript,Ruby On Rails,Mongoid,我是RubyonRails 5的新手,我正在尝试制作一个“调查应用程序”,使用户能够将问题作为动态表单动态添加到调查中。我设法让它运行,但添加问题按钮不添加问题表单,删除按钮不删除问题表单 填写字段并单击submit按钮后,它只保存我在survey model中指定的usergroup属性 任何人都可以帮助表单添加更多的问题,并能够删除它的用户 models/survey.rb models/question.rb 控制器/测量控制器.rb 调查/form.html.erb 剧本 为什么不遵循
group
属性
任何人都可以帮助表单添加更多的问题,并能够删除它的用户
models/survey.rb
models/question.rb
控制器/测量控制器.rb
调查/form.html.erb
剧本
为什么不遵循正统的方法()而不是编写自己的helper方法
link\u to\u add\u row
和JavaScript来让它们工作呢?谢谢@JagdeepSingh,github链接帮了大忙。
class Survey
include Mongoid::Document
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions, allow_destroy: true
field :group_id, type: String
end
class Question
include Mongoid::Document
field :qtype, type: String
field :tittle, type: String
field :qline, type: String
belongs_to :survey, optional: true
end
def new
@survey = Survey.new
@survey.questions.build
end
def create
@survey = Survey.new(survey_params)
respond_to do |format|
if @survey.save
format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
format.json { render :show, status: :created, location: @survey }
else
format.html { render :new }
format.json { render json: @survey.errors, status: :unprocessable_entity }
end
end
end
<%= form_for(@survey) do |f| %>
<div class="field-input">
<%= f.label :group_id %><br>
<%= f.text_field :group_id %>
</div>
<table class='table'>
<thead>
<tr>
<th></th>
<th>question type</th>
<th>tittle</th>
<th>write question</th>
</tr>
</thead>
<tbody class='fields'>
<%= f.fields_for :questions do |builder| %>
<%= render 'question', f: builder %>
<% end %>
</tbody>
</table>
<div class="actions">
<%= f.button :submit %>
<%= link_to_add_row('Add Question', f, :questions, class: 'btn btn-
primary') %>
</div>
<% end %>
<tr>
<td>
<%= f.hidden_field :_destroy, as: :hidden %>
<%= link_to 'Delete', '#', class: 'remove_record' %>
</td>
<td><%= f.text_field :qtype, label: true %></td>
<td><%= f.text_field :tittle, label: false %></td>
<td><%= f.text_field :qline, label: false, as: :string %></td>
</tr>
module ApplicationHelper
def link_to_add_row(name, f, association, **args)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize, f: builder)
end
link_to(name, 'new', class: "add_fields " + args[:class], data: {id: id, fields: fields.gsub("\n", "")})
end
end
$(document).on('turbolinks:load', function() {
$('form').on('click', '.remove_record', function(event) {
$(this).prev('input[type=hidden]').val('1');
$(this).closest('tr').hide();
return event.preventDefault();
});
$('form').on('click', '.add_fields', function(event) {
var regexp, time;
time = new Date().getTime();
regexp = new RegExp($(this).data('id'), 'g');
$('.fields').append($(this).data('fields').replace(regexp, time));
return event.preventDefault();
});
});