[26]プロジェクトビュー << [ホームに戻る] >> [28]動画(Wistia)
Markdown関数を利用できるように実装していきます。
Markdown関数の詳しい使用方法は下記のページを参照して下さい。
guides.github.com
記述追加 GemFile(89行目)
gem 'redcarpet', '~> 3.5' gem 'coderay', '~> 1.1', '>= 1.1.3'
GemFile
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.3' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 6.0.3', '>= 6.0.3.1' # Use postgresql as the database for Active Record gem 'pg', '>= 0.18', '< 2.0' # Use Puma as the app server gem 'puma', '~> 4.1' # Use SCSS for stylesheets gem 'sass-rails', '>= 6' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 4.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.2', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '~> 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] # Bulma gem 'bulma-rails', '~> 0.7.4' gem 'bulma-extensions-rails', '~> 1.0.30' #デバイス gem 'devise' # 日本語化 gem 'rails-i18n' # google認証 gem 'omniauth' gem 'omniauth-google-oauth2' # アマゾンS3 gem "aws-sdk" #管理ダッシュボード gem 'trestle', '=0.8.12' gem 'trestle-auth', '=0.2.5' # trestle検索 gem 'trestle-search', '~> 0.3.0' # trestle画像アップロードと表示 gem 'trestle-active_storage', '~> 2.2', '>= 2.2.1' gem "mini_magick" gem 'image_processing', '~> 1.2' # trestleリッチテキスト gem 'trestle-tinymce', '~> 0.1.3' # Markdown関数 gem 'redcarpet', '~> 3.5' gem 'coderay', '~> 1.1', '>= 1.1.3'
コマンド
bundle
「app\helpers\application_helper.rb」ファイルの記述を以下のように編集します。
記述追加 app\helpers\application_helper.rb(21行目)
class CodeRayify < Redcarpet::Render::HTML def block_code(code, language) CodeRay.scan(code, language).div(:line_numbers => :table) end end def markdown(text) coderayified = CodeRayify.new(:filter_html => true, :hard_wrap => true) options = { :fenced_code_blocks => true, :no_intra_emphasis => true, :autolink => true, :strikethrough => true, :lax_html_blocks => true, :superscript => true } markdown_to_html = Redcarpet::Markdown.new(coderayified, options) markdown_to_html.render(text).html_safe end
app\helpers\application_helper.rb
module ApplicationHelper def avatar_url(user) if user.avatar.attached? url_for(user.avatar) elsif user.image? user.image else ActionController::Base.helpers.asset_path('icon_default_avatar.jpg') end end def project_cover(project) if project.images.attached? url_for(project.images[0]) else ActionController::Base.helpers.asset_path('icon_default_image.jpg') end end class CodeRayify < Redcarpet::Render::HTML def block_code(code, language) CodeRay.scan(code, language).div(:line_numbers => :table) end end def markdown(text) coderayified = CodeRayify.new(:filter_html => true, :hard_wrap => true) options = { :fenced_code_blocks => true, :no_intra_emphasis => true, :autolink => true, :strikethrough => true, :lax_html_blocks => true, :superscript => true } markdown_to_html = Redcarpet::Markdown.new(coderayified, options) markdown_to_html.render(text).html_safe end end
「app\views\task\show.html.erb」ファイルを編集します。
記述編集 app\views\task\show.html.erb
<section class="section"> <div class="container"> <div class="col m8"> <div class="card z-depth-2"> <%= @task.video %> <div class="row"> </div> <div class="card-action grey-text text-darken-4"> <div class="container"> <div class="notification"> <%= @task.title %> </div> </div> <div class="box"> <span class="tag is-white is-large"> <%= @task.description %></span> </div> <% if !@task.note.blank? %> <div class="box"> <%= markdown(@task.note) %> </div> <% end %> </div> </div> </div> <!-- タスク Section --> <div class="box"> <article class="media"> <div class="media-content"> <div class="content"> <div class="collection"> <% @tasks.each do |task| %> <% if task.header %> <br/> <div class="collection-item active"> <span class="tag is-link is-large"> <%= task.title %></span> <span class="tag is-white is-large"> <%= task.description %></span> </div> <br/> <% else %> <div class="collection-item active"> <span class="tag is-light is-medium" style="margin: 3px;"> <%= link_to [task.project, task], class: "collection-item" do %> <%= task.title %> <% end %> </span> </div> <% end %> <% end %> </div> </div> </div> </article> </div> </div> </section>
以下のようなMarkdown記法が利用できます。
タスクテーブルの「note」フィールドに記述を入力して下さい。
ルビーのコードを書く場合 <br> ```ruby def index @projects = Project.all end def show @project = Project.find(params[:id]) @tasks = @project.tasks end ``` <br> Javascriptを書く場合 <br> ```javascript function fancyAlert(arg) { if(arg) { $.facebox({div:'#foo'}) } } ```
ブラウザ確認
http://localhost:3000/project/1/task/3
まだビデオ表示を実装していませんが、タスクの詳細が見れるようになりました。
↓↓クリックして頂けると励みになります。