LoginSignup
74

More than 5 years have passed since last update.

Rails Markdown サンプルアプリ

Last updated at Posted at 2014-05-26

環境
- Rails 4.1

サンプル
- https://github.com/usutani/note_md

参考
ASCIIcasts - “Episode 272 - RedcarpetでMarkdown”
masuidrive/open-wripe
vmg/redcarpet

実装

Markdownで記述できるメモアプリを作成したいと思います。

rails _4.1.1_ new note_md
cd note_md
rails g scaffold Page body:text
rake db:migrate

Markdownの処理に用いるgemを追加します。

Gemfile
# markdown
gem "redcarpet", "~> 2.3.0"
bundle install

Markdownの処理を追加します。

app/helpers/application_helper.rb
module ApplicationHelper  
  @@markdown = Redcarpet::Markdown.new Redcarpet::Render::HTML,
    autolink: true,
    space_after_headers: true,
    no_intra_emphasis: true,
    fenced_code_blocks: true,
    tables: true,
    hard_wrap: true,
    xhtml: true,
    lax_html_blocks: true,
    strikethrough: true

  def html(markdown)
    @@markdown.render(markdown).html_safe
  end
end
app/views/pages/show.html.erb
<%= html @page.body %>

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
74