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

Gui*_*ume 11 ruby-on-rails best-in-place

我正在使用best_in_place gem在Rails应用程序中进行一些内联​​编辑.

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

<%= best_in_place @myobject, :description, :type => :textarea %>
Run Code Online (Sandbox Code Playgroud)

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

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

<%= best_in_place @myobject, :description, :type => :textarea, :display_with => :simple_format %>
Run Code Online (Sandbox Code Playgroud)

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

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


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

Dan*_*and 7

我有同样的问题并通过绑定到best_in_place文档中描述的"ajax:success"事件并将新行转换为<br />'s 来解决它 .

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

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

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

更新的javascript:

$('.best_in_place').bind('ajax:success', function(){ $(this).JQtextile('textile', this.innerHTML) });
Run Code Online (Sandbox Code Playgroud)

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

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

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

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