简体   繁体   English

将已编辑的值contenteditable字段从视图传递到Rails中的控制器

[英]Passing edited value contenteditable field from view to controller in Rails

I am using Rails 3.1 to build a web form which, among other fields, contains a table whose cells can be edited. 我正在使用Rails 3.1来构建一个Web表单,其中包含一个表格,其中可以编辑单元格。 I require to save the text of edited cell in a database. 我需要将已编辑单元格的文本保存在数据库中。

I am thinking of using HTML5 attribute 'contenteditable'. 我正在考虑使用HTML5属性'contenteditable'。 I can use the value of innerHTML of the cell . 我可以使用单元格的innerHTML值。 How can I pass this value to Rails controller (from the view)? 如何将此值传递给Rails控制器(从视图中)? Is it possible to use params to pass the data? 是否可以使用params传递数据? Any suggestions? 有什么建议么?

HTML containers marked as contenteditable won't automatically be passed along to the server with a form submission, so you'll likely have to use javascript to pull the content out of those containers and pass them along in some fashion. 标记为contenteditable HTML容器不会自动通过表单提交传递给服务器,因此您可能必须使用javascript将内容从这些容器中提取出来并以某种方式传递它们。

One strategy would be to add a handler to the form that fires before submit, which iterates over contenteditable containers and injects hidden form elements. 一种策略是在提交之前触发的表单中添加一个处理程序,它迭代可信容器并注入隐藏的表单元素。 The below example uses jQuery, but you could probably replicate the thought process without. 下面的示例使用jQuery,但您可能无需复制思考过程。

var form = $('#my_form');
var element;
$('[contenteditable=true]').each(function(){
  element = $(this);
  form.append($('<input/>', {
    type:'hidden',
    name:element.attr('id'),
    value:element.html()
  }));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM