noooooob
noooooob

Reputation: 241

Rails 6 ActionText Error undefined method `body' for "":String

I get the following error when I try viewing the posts form on the browser on Rails 6. I have a admin/posts_controller.rb. The posts have a title and a body and I would like to add actiontext using trix editor on the body

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>

I am rendering using the layout admin.html.erb instead of application.html.erb

Here are my relevant files

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"

I am really at my limit here. I am not aware of what I may be missing. Let me know if I require to provide any further details

Upvotes: 1

Views: 1527

Answers (1)

noooooob
noooooob

Reputation: 241

For anyone who comes across this. I have realized the cause of this issue

When using ActionText you should not add a corresponding column to your table.

i.e. You should not have a body column on your posts table if you want to add actiontext on body.

Simply call has_rich_text :body in your model and make sure you have no body column on your posts table

So in this instance, the solution was to remove my body column with the following migration and that was it

Remove Body Column from Posts Migration

remove_column :posts, :body

Upvotes: 5

Related Questions