4

I have application which is using globalize gem and i planning to start using ActionText. So, i have model called Business

class Business < ApplicationRecord
  translates :name, :description
  globalize_accessors

  has_rich_text :description
end 

and i created an record in database. But on attempt to edit i see following error

undefined method `body' for "<div><strong>jfgjwhgewr</strong></div>":String

for my form

  .form-inputs
    - I18n.available_locales.each do |locale|
      = f.input :"name_#{locale.to_s}", label: "Name (#{locale.to_s})"
    - I18n.available_locales.each do |locale|
      = f.label "Description (#{locale.to_s})"
      = f.rich_text_area :"description_#{locale.to_s}"

Whats wrong with it and how can i solve this issue?

PS: I found https://github.com/rails/actiontext/issues/32#issuecomment-450653800 but this solution looks little bit strange :(

Alexey Poimtsev
  • 2,845
  • 3
  • 35
  • 63

2 Answers2

2

I solved in this way

 class Post < ApplicationRecord
   translates :title, :body, touch: true

   delegate :body, to: :translation
   delegate :body=, to: :translation

   after_save do
      body.save if body.changed?
   end

   class Translation
      has_rich_text :body
   end
sparkle
  • 7,530
  • 22
  • 69
  • 131
  • I had to use `content.save if content_changed?` because `content.changed?` always gives false back. I use `content` instead of `body` to not getting confused with `ActionText::RichText#body`. – Gerald Müller Feb 26 '21 at 10:02
  • discussion: https://github.com/rails/actiontext/issues/32#issuecomment-450653800 – Bergrebell Mar 10 '23 at 10:14
1

suppose you use globalize gem and did the migration according to globalize docs ->

class CreateTranslation < ActiveRecord::Migration[6.0]
  def up
    Business.create_translation_table!({name: :string,  description: text}, { migrate_data: true })
  end

  def down
    Business.drop_translation_table! migrate_data: true
  end
end

remove from model:

globalize_accessors
has_rich_text :description

and try to use the following in the view:

<%= rich_text_area_tag "business[name]",  f.object.name %>
<%= rich_text_area_tag "business[description]",  f.object.description %>
ABA
  • 39
  • 1
  • 7