问题 使用Rails gem'best_in_place'进行内联编辑 - bug:在textarea上编辑后丢失新行


我在用 best_in_place gem在Rails应用程序中进行内联编辑。

我的对象的一个​​属性是类型 text,我希望它在文本区域进行编辑,所以我这样做了:

<%= best_in_place @myobject, :description, :type => :textarea %>

它可以工作,但是当没有编辑时,所有返回(\ n)都被删除。

我尝试使用simple_format,添加 :display_with => :simple_format 传递给best_in_place的选项:

<%= best_in_place @myobject, :description, :type => :textarea, :display_with => :simple_format %>

未编辑时,新行将按预期显示。但是点击进入版本会被打破,上面会添加一个新的破折号。单击它会显示一个textarea框,但它是空的,并且在那里输入的文本不会保存回我的对象​​。

保存在我的属性中的内容只是纯文本,它不包含任何html。


这个问题(和补丁)似乎与我的问题有关: https://github.com/bernat/best_in_place/pull/111
但是,在应用补丁时(手动,应用于文件) .../gems/best_in_place-1.0.6/spec/spec_helper.rb),我仍然有同样的问题。


10049
2018-03-08 15:12


起源



答案:


我遇到了同样的问题并通过绑定到“ajax:success”事件来解决它,如中所述 best_in_place文档 并将新行转换为 <br />的。

$('.best_in_place').bind('ajax:success', function(){ this.innerHTML = this.innerHTML.replace(/\n/g, '<br />') });

您也可以选择使用轻量级标记语言,如纺织品或降价,而不是简单的换行符。可以找到这些Javascript转换器 在这里(纺织品) 和 在这里(降价)

我选择了Textile,因为我可以在best_in_place的display_with选项中使用textilize方法。

更新的javascript:

$('.best_in_place').bind('ajax:success', function(){ $(this).JQtextile('textile', this.innerHTML) });

此外,如果您只想在best_in_place textareas上执行此操作,则可以检查data-type属性:

$('.best_in_place').bind('ajax:success', function(){ 
    if ($(this).attr('data-type') == 'textarea')
        $(this).JQtextile('textile', this.innerHTML) 
});

最后,匹配服务器端的转换:

:display_with => lambda { |v| textilize(v).html_safe } // the RedCloth gem may be required.

7
2018-05-25 06:32



我发现.html_safe在内容(v)为零时抛出错误,所以我测试了它 :display_with => lambda { |v| v.nil? ? '' : textilize(v).html_safe } - Adam Pearlman


答案:


我遇到了同样的问题并通过绑定到“ajax:success”事件来解决它,如中所述 best_in_place文档 并将新行转换为 <br />的。

$('.best_in_place').bind('ajax:success', function(){ this.innerHTML = this.innerHTML.replace(/\n/g, '<br />') });

您也可以选择使用轻量级标记语言,如纺织品或降价,而不是简单的换行符。可以找到这些Javascript转换器 在这里(纺织品) 和 在这里(降价)

我选择了Textile,因为我可以在best_in_place的display_with选项中使用textilize方法。

更新的javascript:

$('.best_in_place').bind('ajax:success', function(){ $(this).JQtextile('textile', this.innerHTML) });

此外,如果您只想在best_in_place textareas上执行此操作,则可以检查data-type属性:

$('.best_in_place').bind('ajax:success', function(){ 
    if ($(this).attr('data-type') == 'textarea')
        $(this).JQtextile('textile', this.innerHTML) 
});

最后,匹配服务器端的转换:

:display_with => lambda { |v| textilize(v).html_safe } // the RedCloth gem may be required.

7
2018-05-25 06:32



我发现.html_safe在内容(v)为零时抛出错误,所以我测试了它 :display_with => lambda { |v| v.nil? ? '' : textilize(v).html_safe } - Adam Pearlman


找到了半工作的解决方案。

show.html.erb

<%= best_in_place @myobject, :description, :type => :textarea, :display_as => 'description_format'  %>

并在 myobject.rb

def description_format
  self.description.gsub(/\n/, "<br>")
end

它按预期工作。几乎。
唯一剩下的问题是:编辑文本时,在从textarea取消聚焦后,新行将再次丢失。如果刷新页面,则会再次正确显示。


4
2018-03-09 11:17





如果 \n 被替换为 <br> 并且用户选择进行更多修改,用户将只在一行上看到所有文本,使得阅读和编辑更加困难。

基于上面的答案,我做了这个解决方案,删除任何 \r 成功后。

$('.best_in_place').bind('ajax:success', function(){ 
    if ($(this).attr('data-type') == 'textarea') {
        this.innerHTML = this.innerHTML.replace(/\r/g, '')
    }
});

这可确保不显示多余的行。此解决方案的优点是,如果用户选择再次编辑该字段,则所有内容都显示为之前。


1
2017-11-06 20:17





我认为这里的答案都有效。这只是另一种选择。您可以在a之间添加best_in_place字段 <pre> 标签,它将负责添加行。当然,需要进行一些格式化和样式更改,但它很容易解决手头的问题。


0
2018-02-10 11:00





在回答以下问题时,有关更新后丢失行格式的问题。经过一些实验,我得到了这个工作。这是一个名为“comment”的字段的示例,它用作textarea并作为类型文本存储在数据库中。

形式如下:

div class: "single-spacing", id: "comment_div" do
    best_in_place coursedate, :comment, as: :textarea, url: [:admin,coursedate], ok_button: "Uppdatera", cancel_button: "Cancel", class: "editable", 
     :display_with => lambda { |c| simple_format(c.gsub("<br />", ""), {}, sanitize: false) }
end

在CSS中:

.single-spacing {
  ul br {
   display: none;
 }
  ol br {
  display: none;
 }
  div br {
   display: none;
  }
h3 {
  border-bottom: 1px dotted #e8e8e8;
   padding-bottom: 15px;
}
 blockquote {
   border-color: #a7c2d9;
    p {
      font-size: 14px;
      color: #777777;
      line-height: 1.5;
    }
  }
}

CoffeeScript的:

# refresh textarea bip on coursedate when edited to reload line breaks after update
  $('#comment_div').bind 'ajax:success', ->
  $('#comment_div').load(document.URL +  ' #comment_div');
  return

0
2017-08-28 15:58





这对我有用。

  $('.best_in_place').bind 'ajax:success', -> 
    content = $(this).text().replace(/\n/g, "<br>")
    $(this).html(content)

或者Jquery

$('.best_in_place').bind('ajax:success', function(){ 
    content = $(this).text().replace(/\n/g, "<br>")
    $(this).html(content)
});

0
2017-10-24 15:47