我有父模型Intervention
,后者又接受模型ExternalFeedback
的嵌套属性。我按照本教程(http://tutorials.pluralsight.com/ruby-ruby-on-rails/ruby-on-rails-nested-attributes)进行了操作,以便我可以根据需要动态添加尽可能多的external_feedbacks,但是当我尝试使用_form
中呈现的external_feedback和另一个动态创建的external_feedback来保存父表单时,rails会给出错误:"Unpermitted parameters: 0, feedback_form_1"
,仅保存父级的属性。
在application.js
中,我创建了一个new_id
变量,以便为字段提供更简单的ID。
只有在使用jQuery添加额外字段时才会发生此错误。如果我没有在表单中添加额外字段,则rails会将数据正确保存到数据库,从而保存唯一创建的external_feedback
。
任何人都可以帮助我吗?我尝试了很多解决方案而没有任何作用。有什么不对?非常感谢你!
以下是相关代码:
日志控制台中的参数现在就像这些(已更新)(通过将feedback_form
代码括在div中来解决以前创建额外_feedbacks.html.erb
属性的问题id="feedback_form"
):
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"
}
intervention.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
答案 0 :(得分:2)
这应该早点跳出来。它与您的哈希键相关联。 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() );
我确实需要更改点击处理程序以使其正常启动,但我认为这是我的设置。添加字段的click事件处理程序对我来说就像这样:
$(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();
});