3

我有使用globalize gem的应用程序,我计划开始使用 ActionText。所以,我有一个名为 Business 的模型

class Business < ApplicationRecord
  translates :name, :description
  globalize_accessors

  has_rich_text :description
end 

我在数据库中创建了一条记录。但是在尝试编辑时我看到以下错误

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

对于我的表格

  .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}"

它有什么问题,我该如何解决这个问题?

PS:我找到了https://github.com/rails/actiontext/issues/32#issuecomment-450653800但这个解决方案看起来有点奇怪:(

4

2 回答 2

1

假设您使用 globalize gem 并根据 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

从模型中移除:

globalize_accessors
has_rich_text :description

并尝试在视图中使用以下内容:

<%= rich_text_area_tag "business[name]",  f.object.name %>
<%= rich_text_area_tag "business[description]",  f.object.description %>
于 2020-02-26T08:43:41.580 回答
1

我是这样解决的

 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
于 2020-03-11T16:30:51.600 回答