【问题标题】:Covert "back" to textile markup when editing post via RedCloth and Acts-as-Textiled?通过 RedCloth 和 Acts-as-Textiles 编辑帖子时隐蔽“返回”到纺织品标记?
【发布时间】:2010-11-29 18:34:10
【问题描述】:

问题:如何将我的纺织品生成的 HTML “转换”回我的应用程序中的纺织品标记?

情况是这样的。通过在我的“帖子”模型中实现 RedCloth 和作为纺织物,我设法在我的 Rails 3 应用程序中拼凑出一个穷人的博客编辑器。我使用acts-as-textiled 来减轻在我的“帖子”正文中编写内容的痛苦。

效果很好。但是,当我编辑帖子时,我会在帖子正文中看到生成的 HTML。我想看到的是原始的纺织品标记。这可能吗?

提前感谢您的帮助!

这是我的帖子模型:

class Post < ActiveRecord::Base
  has_many :tags
  acts_as_taggable_on :tags
  acts_as_textiled :title, :body
  attr_accessible :tag_list, :tags, :title, :body, :post, :comments
  validates :title, :presence => true
  validates :body, :presence => true
  default_scope :order => 'posts.created_at DESC'

这是我的“显示”方法的帖子视图:

<%= @post.title.html_safe %>
<%= @post.body.html_safe %>

【问题讨论】:

  • 所以我找到了一个名为“ClothRed”的宝石,但我终生无法弄清楚如何实现它。

标签: ruby-on-rails textile redcloth


【解决方案1】:

试试这个:

html = <<HTML
<h1>This is a heading</h1>
<strong>bold text</strong>
<i>italic</i>
HTML

textile = ClothRed.new(html).to_textile

puts textile

输出:

h1. This is a heading


*bold text*  
__italic__

在你的情况下,我认为你应该这样做:

<%= ClothRed.new(@post.body).to_textile.html_safe %>
# instead of
<%= @post.body.html_safe %>

不过,我不知道这是否被认为是最佳做法。

文档:http://clothred.rubyforge.org/doc/html/index.html

【讨论】:

  • 这行得通,但我认为最好使用像 MarkItUp 这样的嵌入式纺织品编辑器,而不是每次查看或编辑帖子时都从纺织品来回转换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 2010-12-16
  • 2014-06-15
相关资源
最近更新 更多