💎

Rails に Action Text を追加する

2025/02/24に公開

お家の検証サーバ用の備忘録です。

前提

Active Storage のインストールは終わっている状態です。

https://zenn.dev/asterisk9101/articles/ruby_on_rails8-2

文書にリッチテキストコンテンツを追加する

ActionText を追加します。

bundle exec rails action_text:install
bundle exec rails db:migrate

モデルに関連を追記します。

vi app/models/document.rb
has_rich_text :content

ビューにリッチテキストを表示できるよう追記します。

vi app/views/documents/_form.html.erb
<div>
  <%= form.label :content, style: "display: block" %>
  <%= form.rich_textarea :content %>
</div>
vi app/views/documents/_document.html.erb
<p>
  <strong>Content:</strong>
  <%= document.content %>
</p>

コントローラに追加します。

vi app/controllers/documents_controller.rb
- params.expect(document: [ :name, :status, :due_date, :section_id, attachments: [] ])
+ params.expect(document: [ :name, :status, :due_date, :section_id, :content, attachments: [] ])

画像の表示

Action Text に添付した画像は Active Storage に格納されるので、認証機構に追記が必要。

vi app/controllers/active_storage/base_controller.rb
def authorized?
  redirect_to "/" unless current_user

  allow = @blob.attachments.any? do |attachment|
    type = attachment.record_type
    logger.debug type
    id   = attachment.record_id
    record = type.constantize.find(id)
+    if record.class.to_s == "ActionText::RichText"
+      record = record.record
+    end
    record.allow?(current_user)
  end

  redirect_to "/" unless allow
end

次(予定)

https://zenn.dev/asterisk9101/articles/ruby_on_rails8-4

GitHubで編集を提案

Discussion