Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 保存动态创建的字段时不允许使用Rails参数_Javascript_Jquery_Ruby On Rails_Ruby - Fatal编程技术网

Javascript 保存动态创建的字段时不允许使用Rails参数

Javascript 保存动态创建的字段时不允许使用Rails参数,javascript,jquery,ruby-on-rails,ruby,Javascript,Jquery,Ruby On Rails,Ruby,我有父模型干预,它依次接受模型外部反馈的嵌套属性。我遵循了本教程(),以便可以根据需要动态添加尽可能多的外部反馈,但是当我尝试使用\u表单中呈现的外部反馈保存父表单以及动态创建的另一个表单时,rails给出了错误:“未经允许的参数:0,反馈\u表单1”,只保存父表单的属性。 在application.js中,我创建了一个new_id变量,为字段提供更简单的id 只有在使用jQuery添加额外字段时,才会发生此错误。如果我不向表单中添加额外字段,rails会将数据正确地保存到DB,只保存创建的外部

我有父模型
干预
,它依次接受模型
外部反馈的嵌套属性
。我遵循了本教程(),以便可以根据需要动态添加尽可能多的外部反馈,但是当我尝试使用
\u表单
中呈现的外部反馈保存父表单以及动态创建的另一个表单时,rails给出了错误:
“未经允许的参数:0,反馈\u表单1”
,只保存父表单的属性。 在
application.js
中,我创建了一个
new_id
变量,为字段提供更简单的id

只有在使用jQuery添加额外字段时,才会发生此错误。如果我不向表单中添加额外字段,rails会将数据正确地保存到DB,只保存创建的
外部反馈

有人能帮我吗?我尝试了这么多的解决方案,但都不管用。有什么不对劲?提前非常感谢

以下是相关代码:

日志控制台中的参数现在是这样的(已更新)(解决了以前创建额外的
反馈表单
属性的问题,方法是将
\u feedbacks.html.erb
代码包含在一个div中,并带有
id=“feedback\u form”
):

}

干预。rb:

 class Intervention < ApplicationRecord

  belongs_to :incident
  belongs_to :user

  belongs_to :incident_priority
  belongs_to :intervention_status

  validates_presence_of :user_id, :incident_id

  has_many :external_feedbacks, :inverse_of => :intervention, dependent: :destroy
  accepts_nested_attributes_for :external_feedbacks, :allow_destroy => true, :reject_if => :all_blank

end
<div id="feedback-forms">
  <%= f.fields_for :external_feedbacks do |fb| %>

      <%= render 'feedbacks', f: fb %>

  <% end %>
</div>

<div class="actions" align="right">
  <%= link_to_add_fields 'Adicionar novo feedback', f, :external_feedbacks %>
  <%= link_to 'Cancelar', incidents_path(:mirth => @mirth, :project => @project), class: "btn btn-info" %>
  <%= f.submit "Gravar", class: "btn btn-info" %>
</div>
def link_to_add_fields(name = nil, f = nil, association = nil, options = nil, html_options = nil, &block)
    # If a block is provided there is no name attribute and the arguments are
    # shifted with one position to the left. This re-assigns those values.
    f, association, options, html_options = name, f, association, options if block_given?

    options = {} if options.nil?
      html_options = {} if html_options.nil?

      if options.include? :locals
        locals = options[:locals]
      else
        locals = {}
    end

    if options.include? :partial
      partial = options[:partial]
    else
      partial = 'feedbacks'
    end

    # Render the form fields from a file with the association name provided
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, child_index: 'feedback_form') do |builder|
      render(partial, locals.merge!(f: builder))
    end

    # The rendered fields are sent with the link within the data-form-prepend attr
    html_options['data-form-prepend'] = raw CGI::escapeHTML(fields)
    html_options['href'] = '#'

    content_tag(:a, name, html_options, &block)
end
$("div#feedback-forms").on('click', '.remove-feedback', function (event) {

    event.preventDefault(); // Prevent link from following its href

    $(this).closest("[id^=feedback-form]").remove(); //procura div com id 'feedback-form*'

});

var new_id = 1;  


$('[data-form-prepend]').click( function(e) {

    var obj = $( $(this).attr('data-form-prepend') );

    obj.find('input, select, textarea').each( function() {
        $(this).attr( 'name', function() {
            //return $(this).attr('name').replace( 'new_record', (new Date()).getTime() );
            return $(this).attr('name').replace( 'feedback_form', 'feedback_form_' + new_id );
        });
    });

    obj.insertBefore( this );

    new_id++;

    return false;
});
def new


@incident = Incident.find(params[:incident])
@user = User.find(current_user.id)
@intervention = Intervention.new(user: @user, incident: @incident)
@intervention.external_feedbacks.build

.....

end


def create

    @incident = Incident.find(params[:incident_id])
    @user = User.find(params[:user_id])
    @intervention = Intervention.create(intervention_params)
    @intervention.incident = @incident
    @intervention.user = @user

....

end

.....

def intervention_params
    params.require(:intervention).permit(:id, :user_id, :incident_id, :incident_priority_id, :begin_date, :end_date, :description,
                                         :intervention_status_id, :forwarded_to,
                                         external_feedbacks_attributes: [:id, :date, :feedback_source, :external_ticket,
                                                                         :feedback, :intervention_id, :_destroy])

  end
\u feedbacks.html.erb(更新):

<div id="feedback_form">
  <div class="form-group form-inline">
    <%= f.label 'Data' %>
    <%= f.datetime_select :date, class: "form-control" %>
  </div>

  <div class="form-group form-inline">
    <%= f.label 'Origem da informação' %>
    <%= f.text_field :feedback_source, class: "form-control" %>
  </div>

  <div class="form-group form-inline">
    <%= f.label '# Ticket' %>
    <%= f.text_field :external_ticket, class: "form-control" %>
  </div>

  <div class="form-group form-inline">
    <%= f.label :feedback %>
    <%= f.text_area :feedback, rows: 4, class: "form-control" %>
  </div>

  <%= f.hidden_field :_destroy %>
  <%= link_to 'Apagar', '#', class: 'remove-feedback' %>

  <hr>
  <br>
</div>
干预措施\u controller.rb:

 class Intervention < ApplicationRecord

  belongs_to :incident
  belongs_to :user

  belongs_to :incident_priority
  belongs_to :intervention_status

  validates_presence_of :user_id, :incident_id

  has_many :external_feedbacks, :inverse_of => :intervention, dependent: :destroy
  accepts_nested_attributes_for :external_feedbacks, :allow_destroy => true, :reject_if => :all_blank

end
<div id="feedback-forms">
  <%= f.fields_for :external_feedbacks do |fb| %>

      <%= render 'feedbacks', f: fb %>

  <% end %>
</div>

<div class="actions" align="right">
  <%= link_to_add_fields 'Adicionar novo feedback', f, :external_feedbacks %>
  <%= link_to 'Cancelar', incidents_path(:mirth => @mirth, :project => @project), class: "btn btn-info" %>
  <%= f.submit "Gravar", class: "btn btn-info" %>
</div>
def link_to_add_fields(name = nil, f = nil, association = nil, options = nil, html_options = nil, &block)
    # If a block is provided there is no name attribute and the arguments are
    # shifted with one position to the left. This re-assigns those values.
    f, association, options, html_options = name, f, association, options if block_given?

    options = {} if options.nil?
      html_options = {} if html_options.nil?

      if options.include? :locals
        locals = options[:locals]
      else
        locals = {}
    end

    if options.include? :partial
      partial = options[:partial]
    else
      partial = 'feedbacks'
    end

    # Render the form fields from a file with the association name provided
    new_object = f.object.class.reflect_on_association(association).klass.new
    fields = f.fields_for(association, new_object, child_index: 'feedback_form') do |builder|
      render(partial, locals.merge!(f: builder))
    end

    # The rendered fields are sent with the link within the data-form-prepend attr
    html_options['data-form-prepend'] = raw CGI::escapeHTML(fields)
    html_options['href'] = '#'

    content_tag(:a, name, html_options, &block)
end
$("div#feedback-forms").on('click', '.remove-feedback', function (event) {

    event.preventDefault(); // Prevent link from following its href

    $(this).closest("[id^=feedback-form]").remove(); //procura div com id 'feedback-form*'

});

var new_id = 1;  


$('[data-form-prepend]').click( function(e) {

    var obj = $( $(this).attr('data-form-prepend') );

    obj.find('input, select, textarea').each( function() {
        $(this).attr( 'name', function() {
            //return $(this).attr('name').replace( 'new_record', (new Date()).getTime() );
            return $(this).attr('name').replace( 'feedback_form', 'feedback_form_' + new_id );
        });
    });

    obj.insertBefore( this );

    new_id++;

    return false;
});
def new


@incident = Incident.find(params[:incident])
@user = User.find(current_user.id)
@intervention = Intervention.new(user: @user, incident: @incident)
@intervention.external_feedbacks.build

.....

end


def create

    @incident = Incident.find(params[:incident_id])
    @user = User.find(params[:user_id])
    @intervention = Intervention.create(intervention_params)
    @intervention.incident = @incident
    @intervention.user = @user

....

end

.....

def intervention_params
    params.require(:intervention).permit(:id, :user_id, :incident_id, :incident_priority_id, :begin_date, :end_date, :description,
                                         :intervention_status_id, :forwarded_to,
                                         external_feedbacks_attributes: [:id, :date, :feedback_source, :external_ticket,
                                                                         :feedback, :intervention_id, :_destroy])

  end

这本该早点冲我来的。它与你的散列键绑定。Rails需要整数值,但在JS中,您将其作为
new\u id\u[integer]
传递。看看这个

在application.js中替换此行:

return $(this).attr('name').replace( 'feedback_form', 'feedback_form_' + new_id );
为此:

return $(this).attr('name').replace( 'feedback_form', 'feedback_form_' + new_id );
或者,若要更仔细地阅读教程,您可以使用以下内容:

return $(this).attr('name').replace( 'feedback_form', (new Date()).getTime() );
我确实需要更改点击处理程序的一个按钮,以使其正常启动,但我认为这是我的设置。单击事件处理程序添加字段的结果如下:

$(document).on('click', '*[data-form-prepend]', function(e) {
  var obj = $( $(this).attr('data-form-prepend') );
  obj.find('input').each( function() {
    $(this).attr( 'name', function() {
      return $(this).attr('name').replace( 'feedback_form', (new Date()).getTime() );
    });
  });
  obj.insertBefore( $(this) );
  return e.preventDefault();
});

检查呈现的html时,是否有空的反馈表单属性/元素?在js将其转换为子表单之前,您可能有一个正在提交的表单。我在页面中找不到任何其他表单:/I如果您认为更容易解决问题,我可以发送我的文件。我通过封装
\u feedbacks.html.erb
解决了空
反馈表单
属性的问题使用
id=“feedback\u form”
在div中编码(检查上面更新的信息)。当动态生成1个或多个属性时,仍然存在不保存外部\u反馈\u属性的问题。如何更改
interferences\u controller.rb
中的白名单参数以接受动态生成的参数?我看到了教程,但在我的案例中无法使其正常工作。您是否可以删除
,:如果=>:拒绝所有\u blank
,以确保验证不会干扰它。并删除
验证是否存在:用户id、:事件id
。基本上,尝试删除任何验证,看看这是否是原因。如果是的话,有一些方法可以修复它们,使其正常工作。我这样做了,但问题仍然存在。我尝试通过执行
params.require(:intervention.permited)来临时修复以允许质量分配,并正确保存参数。但我真正想要的是动态地设置参数,以允许外部反馈属性,但无法使其工作。我做了:
params.require(:intervention)。permit(:user_id,:incident_id,:incident_priority_id,:begin_date,:end_date,:description,:intervention_status_id,:forwarded_to)。点击do |白名单|白名单|白名单[:外部反馈]=params[:intervention][:external feedbacks]end
谢谢你的回答,他们向我澄清了事情!但我的问题是无法保存动态生成的嵌套属性。我不知道您是否看到了我的最后一条评论,在那里我设置为mass assign属性。但我真正想要的是删除mass赋值,只允许类和嵌套属性,以及动态生成的属性。你知道我该怎么做吗?@Ayanami如果我不明白,很抱歉。上述内容应允许您删除体量指定,并接受干预的属性和外部反馈的嵌套属性。从代码中可以看出,您的干预表单加载了干预属性字段和单个外部反馈字段集。然后,该按钮允许您添加更多外部反馈字段(这些是动态创建的字段)。动态添加的表单字段没有被保存,因为您在javascript中创建了散列键(“feedback\u form\u 1”)。我按照您的建议做了,并解决了这个问题!非常感谢你救了我一天!!:)