- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有父模型 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/
我尝试根据表单元素的更改禁用/启用保存按钮。但是,当通过弹出按钮选择更改隐藏输入字段值时,保存按钮不受影响。 下面是我的代码。我正在尝试序列化旧的表单值并与更改后的表单值进行比较。但我猜隐藏的字段值无
我正在尝试保存模型的实例,但我得到了 Invalid EmbeddedDocumentField item (1) 其中 1 是项目的 ID(我认为)。 模型定义为 class Graph(Docum
我有一个非常奇怪的问题......在我的 iPhone 应用程序中,用户可以打开相机胶卷中的图像,在我的示例中 1920 x 1080 像素 (72 dpi) 的壁纸。 现在,想要将图像的宽度调整为例
目前,我正在使用具有排序/过滤功能的数据表成功地从我的数据库中显示图像元数据。在我的数据表下方,我使用第三方图像覆盖流( http://www.jacksasylum.eu/ContentFlow/
我的脚本有问题。我想按此顺序执行以下步骤: 1. 保存输入字段中的文本。 2. 删除输入字段中的所有文本。 3. 在输入字段中重新加载之前删除的相同文本。 我的脚本的问题是 ug()- 函数在我的文本
任何人都可以帮助我如何保存多对多关系吗?我有任务,用户可以有很多任务,任务可以有很多用户(多对多),我想要实现的是,在更新表单中,管理员可以将多个用户分配给特定任务。这是通过 html 多选输入来完成
我在 Tensorflow 中训练了一个具有批归一化的模型。我想保存模型并恢复它以供进一步使用。批量归一化是通过 完成的 def batch_norm(input, phase): retur
我遇到了 grails 的问题。我有一个看起来像这样的域: class Book { static belongsTo = Author String toString() { tit
所以我正在开发一个应用程序,一旦用户连接(通过 soundcloud),就会出现以下对象: {userid: userid, username: username, genre: genre, fol
我正在开发一个具有多选项卡布局的 Angular 7 应用程序。每个选项卡都包含一个组件,该组件可以引用其他嵌套组件。 当用户选择一个新的/另一个选项卡时,当前选项卡上显示的组件将被销毁(我不仅仅是隐
我尝试使用 JEditorPane 进行一些简单的文本格式化,但随着知识的增长,我发现 JTextPane 更容易实现并且更强大。 我的问题是如何将 JTextPane 中的格式化文本保存到文件?它应
使用 Docker 相当新。 我为 Oracle 11g Full 提取了一个图像。创建了一个数据库并将应用程序安装到容器中。 正确配置后,我提交了生成 15GB 镜像的容器。 测试了该图像的新容器,
我是使用 Xcode 和 swift 的新手,仍在学习中。我在将核心数据从实体传递到文本字段/标签时遇到问题,然后用户可以选择编辑和保存记录。我的目标是,当用户从 friendslistViewCon
我正在用 Java 编写 Android 游戏,我需要一种可靠的方法来快速保存和加载应用程序状态。这个问题似乎适用于大多数 OO 语言。 了解我需要保存的内容:我正在使用策略模式来控制我的游戏实体。我
我想知道使用 fstream 加载/保存某种结构类型的数组是否是个好主意。注意,我说的是加载/保存到二进制文件。我应该加载/保存独立变量,例如 int、float、boolean 而不是结构吗?我这么
我希望能够将 QNetworkReply 保存到 QString/QByteArray。在我看到的示例中,它们总是将流保存到另一个文件。 目前我的代码看起来像这样,我从主机那里得到一个字符串,我想做的
我正在创建一个绘图应用程序。我有一个带有 Canvas 的自定义 View ,它根据用户输入绘制线条: class Line { float startX, startY, stopX, stop
我有 3 个 Activity 第一个 Activity 调用第二个 Activity ,第二个 Activity 调用第三个 Activity 。 第二个 Activity 使用第一个 Activi
我想知道如何在 Xcode 中保存 cookie。我想使用从一个网页获取的 cookie 并使用它访问另一个网页。我使用下面的代码登录该网站,我想保存从该连接获得的 cookie,以便在我建立另一个连
我有一个 SQLite 数据库存储我的所有日历事件,建模如下: TimerEvent *Attributes -date -dateForMark -reminder *Relat
我是一名优秀的程序员,十分优秀!