0

html.erb ファイルで Coderay を使用した経験がある人はいますか? これらのファイルで coderay を使用する方法がわかりません。

私はヘルパーメソッドを持っています

def coderay(text)
  text.gsub(/\<code( lang="(.+?)")?\>(.+?)\<\/code\>/m) do
    CodeRay.scan($3, $2).div(:css => :class)
  end
end

すべてのテキストをモデルに保存し、coderay を使用してフォーマットしたいものをカプセル化します。

<code lang="ruby">
  <div class="sidebarbox-title">
    <h3>Posts</h3>
  </div>
  <% @posts.each do |p| %>
    <h3 class="post-header">
      <%= p.name %>
    </h3>
   <% end %>
</code>

ビューで coderay 形式のテキストを使用する場合は、次のようにします。

<%= coderay(@post.comments).html_safe %>

.html.erbを使用している場合、言語を指定するにはどうすればよいですか?明確にするために

<code lang="**What goes here**">
  <div class="sidebarbox-title">
    <h3>Posts</h3>
  </div>
  <% @posts.each do |p| %>
    <h3 class="post-header">
      <%= p.name %>
    </h3>
   <% end %>
</code>

docs Hereは、それができると述べているようですが、方法がわかりません

ありがとう

4

1 に答える 1