2012-03-08 47 views
11

编辑后,我使用best_in_place宝石做的Rails应用程序的一些行内编辑丢失。使用Rails的宝石“best_in_place”内置编辑 - 错误:新行上textarea的

我的一个对象的属性是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),我仍然有同样的问题。

回答

7

我有同样的问题,并通过结合“AJAX:成功”解决了其作为best_in_place documentation描述的事件和转换新的生产线,以<br />的。

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

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

我与纺织去,因为我可以只使用textilize方法best_in_place的display_with选项。

更新的javascript:

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

另外,如果你只想在best_in_place文字区域这个行为,你可以检查数据类型属性:

$('.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. 
+0

我发现.html_safe被扔一个错误,当该内容(V)为零,所以我测试对于它':display_with =>拉姆达{| V | v.nil? ? '':textilize(v).html_safe}' – 2014-08-27 06:01:44

4

发现了一个半工作解决方案。

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的,新线再次丢失了。如果刷新页面,则会再次正确显示。

1

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

根据上面的答案,我提出了这个解决方案,一旦成功就删除任何\r

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

这确保不会显示额外的行。此解决方案的优点是,如果用户选择再次编辑该字段,则会像以前一样显示所有内容。

0

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

0

针对以下问题,有关更新后丢失行格式的问题。经过一些实验,我得到了这个工作。这是一个称为“注释”的字段的示例,该字段用作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

这奏效了我。

$('.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) 
});