Rails 6 ActionText 错误未定义“”的方法“主体”:字符串

如何解决Rails 6 ActionText 错误未定义“”的方法“主体”:字符串

当我尝试在 Rails 6 上的浏览​​器上查看帖子表单时出现以下错误我有一个 admin/posts_controller.rb。这些帖子有一个标题一个正文,我想在正文中使用 trix 编辑器添加 actiontext

NoMethodError at /admin/posts/new
undefined method `body' for "":String

Hint: `body` is being called on a `String` object,which might not be the type of object you were expecting.

app/models/post.rb

has_rich_text :body
extend FriendlyId
friendly_id :title,use: :slugged
mount_uploader :banner,BannerUploader

admin/posts/_form.html.erb

<div class="form-group">
   <%= form.label :body %>
   <%= form.rich_text_area :body,id: :post_body,class: 'form-control' %>
</div>

我使用布局 admin.html.erb 而不是 application.html.erb

这是我的相关文件

app/packs/admin.js

require("trix")
require("@rails/actiontext")
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
// import the bootstrap javascript module
import "jquery"
import "bootstrap"
import "feather"
import "perfect-scrollbar"
require('./dash/dash')
import "js-cookie"
require("@nathanvda/cocoon")
// Load Datatables
import dt from "datatables.net";
document.addEventListener("turbolinks:load",() => {
    dt(window,$);
});
const dataTables = [];
document.addEventListener("turbolinks:load",() => {
  if (dataTables.length === 0 && $('.data-table').length !== 0) {
    $('.data-table').each((_,element) => {
      dataTables.push($(element).DataTable({
        pageLength: 50
      }));
    });
  }
});
document.addEventListener("turbolinks:before-cache",() => {
  while (dataTables.length !== 0) {
    dataTables.pop().destroy();
  }
});
// Import admin.css files
import '../stylesheets/admin'
require.context('../images/dash',true)
Rails.start()
Turbolinks.start()
ActiveStorage.start()

app/javascripts/stylesheets/admin.scss

@import "actiontext.scss";
@import "dashforge/fontawesome-all.scss";
@import "dashforge/ionicons.scss";
@import "dashforge/dashforge.scss" 

app/javascripts/actiontext.scss

//
// Provides a drop-in pointer for the default Trix stylesheet that will format the toolbar and
// the trix-editor content (whether displayed or under editing). Feel free to incorporate this
// inclusion directly in any other asset bundle and remove this file.
//
//= require trix/dist/trix
// We need to override trix.css's image gallery styles to accommodate the
// <action-text-attachment> element we wrap around attachments. Otherwise,// images in galleries will be squished by the max-width: 33%; rule.
.trix-content {
  .attachment-gallery {
    > action-text-attachment,> .attachment {
      flex: 1 0 33%;
      padding: 0 0.5em;
      max-width: 33%;
    }
    &.attachment-gallery--2,&.attachment-gallery--4 {
      > action-text-attachment,> .attachment {
        flex-basis: 50%;
        max-width: 50%;
      }
    }
  }
  action-text-attachment {
    .attachment {
      padding: 0 !important;
      max-width: 100% !important;
    }
  }
} 

config/application.rb

require "action_text/engine"

我真的到了极限。我不知道我可能会错过什么。如果我需要提供更多详细信息,请告诉我

解决方法

对于遇到此问题的任何人。我已经意识到这个问题的原因

在使用 ActionText 时,您不应在表格中添加相应的列。

即如果您想在 body 上添加操作文本,您的 posts table 上不应有 body 列。

只需在您的模型中调用 has_rich_text :body 并确保您的 body 表中没有 posts

所以在这种情况下,解决方案是通过以下迁移删除我的 body 列,就是这样

Remove Body Column from Posts Migration

remove_column :posts,:body

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?