gemfilegem 'redcarpet'
をbundle install
し、
schemafile
create_table "renewals", force: :cascade do |t| t.string "title" t.text "content" end
上記のモデルを作成し、
application_helper.rb
def markdown(text) options = { filter_html: true, hard_wrap: true, link_attributes: { rel: 'nofollow', target: "_blank" }, space_after_headers: true, fenced_code_blocks: true } extensions = { autolink: true, superscript: true, disable_indented_code_blocks: true } renderer = Redcarpet::Render::HTML.new(options) markdown = Redcarpet::Markdown.new(renderer, extensions) markdown.render(text).html_safe end
というようにヘルパーに記述し、
renewals_controller.rbに
class RenewalsController < ApplicationController def index @renewal = Renewal.all end def new @renewal = Renewal.new end def create @renewal = Renewal.create(renewal_params) redirect_to action: 'show', id: @renewal.id end def show @renewal = Renewal.find(params[:id]) end def edit @renewal = Renewal.find(params[:id]) end def update @renewal = Renewal.find(params[:id]) @renewal.update_attributes(renewal_params) redirect_to action: 'show', id: @renewal.id end private def renewal_params params.require(:renewal).permit(:title, :content) end end
のように記述し、
views/renewals/show.html.erbに
<h1><%= @renewal.title %></h1> <p><%= markdown(@renewal.content) %></p> <%= link_to "ニュース一覧に戻る", renewals_path %>
と書いています。
renewlas/newページでマークダウン記法を用いたtextを入力して、showページで見ようとすると、出力がされないようです。
なにか特別な処理が必要でしたでしょうか。
ルーティングは、
resources :renewals
の記述のみでや新規の記事作成、編集、一覧表示、個々の記事ページ表示などはできており、
<%= markdown(@renewal.content) %>
によって出力されるのは、マークダウンのために記入していた##などのものは覗かれたものになります。
例えば、「##マークダウン」などと入力すると、その表示は「マークダウン」となり、「##」は無視されます。
うまく出力するにはどうしたらいいでしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/16 01:21
退会済みユーザー
2016/04/16 05:43
2016/04/16 06:56
2016/04/16 07:11
2016/04/16 07:20
退会済みユーザー
2016/04/16 08:52 編集
2016/04/16 10:35
退会済みユーザー
2016/04/16 12:53 編集
2016/04/19 05:36
2016/04/19 05:42
退会済みユーザー
2016/04/19 06:11
2016/04/19 08:06
退会済みユーザー
2016/04/19 12:03