【发布时间】:2021-04-14 18:35:51
【问题描述】:
当我尝试在 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"
我真的到了我的极限。我不知道我可能会错过什么。如果我需要提供任何进一步的详细信息,请告诉我
【问题讨论】:
标签: ruby-on-rails ruby actiontext