gpt4 book ai didi

javascript - 保存动态创建的字段时 Rails 不允许的参数

转载 作者:数据小太阳 更新时间:2023-10-29 08:59:14 35 4
gpt4 key购买 nike

我有父模型 Intervention,它又接受模型 ExternalFeedback 的嵌套属性。我遵循了本教程 (http://tutorials.pluralsight.com/ruby-ruby-on-rails/ruby-on-rails-nested-attributes),以便我可以根据需要动态添加尽可能多的 external_feedbacks,但是当我尝试使用 _form 中呈现的 external_feedback 和另一个动态创建的父表单保存父表单时,rails给出错误:“Unpermitted parameters: 0, feedback_form_1”,仅保存父项的属性。在 application.js 中,我创建了一个 new_id 变量来为字段提供更简单的 ID。

此错误仅在我使用 jQuery 添加额外字段时发生。如果我不向表单添加额外的字段,rails 会将数据正确保存到数据库,保存唯一的 external_feedback 创建。

谁能帮帮我?我尝试了很多解决方案,但没有任何效果。有什么问题?非常感谢您!

相关代码如下:

日志控制台中的参数现在像这样(已更新)(解决了之前通过包含 _feedbacks.html 创建额外的 feedback_form 属性的问题。带有 id="feedback_form" 的 div 中的 erb 代码:

Parameters : {
"utf8" => "✓",
"authenticity_token" => "hxbegdsEOu4BiS7cj7TCBvtpNJpQcEKK0AVI8gfF9LZ+RApGwoimq50dctRfN6Wn/yUou6qyLxNUZ6UgXZl8uw==",
"user_id" => "1",
"incident_id" => "62",
"intervention" => {
"incident_priority_id" => "1",
"begin_date(1i)" => "2016",
"begin_date(2i)" => "11",
"begin_date(3i)" => "4",
"begin_date(4i)" => "09",
"begin_date(5i)" => "49",
"end_date(1i)" => "",
"end_date(2i)" => "",
"end_date(3i)" => "",
"end_date(4i)" => "",
"end_date(5i)" => "",
"description" => "dasdsada ss dasd",
"intervention_status_id" => "1",
"forwarded_to" => "",
"external_feedbacks_attributes" => {
"0" => {
"date(1i)" => "2016",
"date(2i)" => "11",
"date(3i)" => "4",
"date(4i)" => "09",
"date(5i)" => "49",
"feedback_source" => "11",
"external_ticket" => "11",
"feedback" => "11",
"_destroy" => "false"
},
"feedback_form_1" => {
"date(1i)" => "2016",
"date(2i)" => "11",
"date(3i)" => "4",
"date(4i)" => "09",
"date(5i)" => "49",
"feedback_source" => "22",
"external_ticket" => "22",
"feedback" => "222",
"_destroy" => "false"
}
}
},
"commit" => "Gravar"

干预.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

干预/_form.html.erb:

<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>

application_helper.rb:

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

_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>

application.js(更新):

$("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;
});

interventions_controller.rb:

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_id_[integer] 传递。看看这个 answer that goes into also .

在您的 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();
});

关于javascript - 保存动态创建的字段时 Rails 不允许的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407642/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com