LoginSignup
1

More than 3 years have passed since last update.

Rails6 Action Text

Posted at

試してみたく ActionTextを使ってみました

こんな感じのシンプルなブログ投稿

スクリーンショット 2019-09-03 12.19.30.png

環境

  • Docker
  • Ruby2.6.3
  • Rails6.0.0
  • Bootstra4

下準備

image_processing

gem 'image_processing'

これがないと画像が表示されませんでした

$ bundle install

action_text

$ rails action_text:install

以下は自動で入ってました。

/packs/application.js
require("trix")
require("@rails/actiontext")

Post modelの作成

カラムはtitleだけ

$ rails g model Post title:string
$ rails db:migrate

PostmodelとActionTextのtableが作成されます

/models/post.rb
class Post < ApplicationRecord

  validates :title, presence: true

  has_rich_text :content #こちらが必要
end

controllerとview

各controllerは省略します。
ストロングパラメータにcontentを追記

/controllers/posts_controller.rb

def post_params
   params.require(:post).permit(:title, :content) 
end

/views/posts/new.html.erb

    <h2 class="text-center">新規投稿</h2>
    <%= form_with model: @post, local:true do |form| %>
        <div class="form-group">
          <%= form.label :title %> <br />
          <%= form.text_field :title, class: "form-control"  %>
        </div>

        <div class="form-group">
          <%= form.label :content %><br />
          <%= form.rich_text_area :content %>
        </div>

        <hr>

        <div class="form-label-group col-md-6 mx-auto">
          <%= form.submit "登録", class: "btn btn-lg btn-primary btn-block" %>
        </div>
    <% end %>


Screen Recording 2019-09-03 at 12.43 PM.gif

 雑感

ここまでの導入は簡単な印象です。
あとはActive StorageとS3の連携が必要になるかと思います。

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
1