如何解决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 举报,一经查实,本站将立刻删除。