我想在Rails中看到textarea,就像javascript一样
我有一个表单,允许用户提交HTML,我只想对文本进行一些更改(例如,用活动记录中的公司名称替换公司名称),然后在文本区域中替换HTML。然而,它打得不太好。无论出于何种原因,试图用POST请求中提交的内容替换textarea都不起作用。什么也没发生 这是我的查看表单:我想在Rails中看到textarea,就像javascript一样,javascript,html,Javascript,Html,我有一个表单,允许用户提交HTML,我只想对文本进行一些更改(例如,用活动记录中的公司名称替换公司名称),然后在文本区域中替换HTML。然而,它打得不太好。无论出于何种原因,试图用POST请求中提交的内容替换textarea都不起作用。什么也没发生 这是我的查看表单: <div class="modal-body"> <div class="form-group row"> <div class="col-sm-12">
<div class="modal-body">
<div class="form-group row">
<div class="col-sm-12">
<%= form_tag(convert_email_template_path(format: :js), method: :post, :authenticity_token => true, id: 'importEmailForm', remote: true) do %>
<%= text_area_tag 'import_email', nil, rows: 10, placeholder: "Paste the headers of your email here.", class: "form-control" %>
<% end %>
</div>
</div>
</div>
<div class="modal-footer">
<%= link_to '<i class="fa fa-download"></i> Import'.html_safe, '#', onclick: "importEmail();", class: "btn btn-success btn-sm" %>
</div>
它基本上在视图中提交表单。我需要捕获此帖子数据,进行一两次更改,然后继续用修改后的内容替换textarea
现在,如果我使用纯javascript执行此操作,效果很好:
#views/templates/convert_email.js.erb
var sourceBody = $('#import_email').val();
var start_position = sourceBody.indexOf("<html>");
var stop_position = sourceBody.indexOf("</html>");
var html = sourceBody.substring(start_position, stop_position);
var fixed_html = html.replace(/(=\n)/g, '');
// Grab body.
var iframe = document.getElementsByClassName("fr-iframe")[0]
var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
// get the element in question
const input = innerDoc.getElementsByTagName("body")[0];
// Before pasting data, let's make sure the code view is turned OFF.
if ($('textarea').froalaEditor('codeView.isActive') == true) {
$('textarea').froalaEditor('codeView.toggle');
}
// dispatch keyboard events
input.innerHTML = fixed_html;
// Close modal.
$('#Modal').modal('toggle')
并将其传递到javascript视图
#views/templates/convert_email.js.erb
// Grab body.
var iframe = document.getElementsByClassName("fr-iframe")[0]
var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
// get the element in question
const input = innerDoc.getElementsByTagName("body")[0];
// log codeview status
var codeView = 0
// Before pasting data, let's make sure the code view is turned OFF.
if ($('textarea').froalaEditor('codeView.isActive') == false) {
codeView = 0;
$('textarea').froalaEditor('codeView.toggle');
} else {
codeView = 1;
}
function
// dispatch keyboard events
input.innerHTML = "<%= @body %>";
resizeFroala();
// Close modal.
$('#Modal').modal('toggle')
但尝试将其写入控制台时如下所示:
function importEmail() {
setTimeout(function () {
$.ajax({
type: 'POST',
url: $("#importEmailForm").attr("action"),
data: $("#importEmailForm").serialize(),
});
}, 100)
};
#controllers/templates.rb
body = params[:import_email]
@body = body.gsub("=\n", "")
对不起,有什么困惑。希望这是有意义的。我基本上只想在Javascript中完美地工作,但在Rails中工作。我想我终于找到了答案。除了
将我的代码更改为:
input.innerHTML=“”代码>
这就成功了。哇
body = params[:import_email]
@body = body.gsub("=\r", "\r").gsub("\n", "").gsub("\r", "").gsub("\t", "")