Ask A Question

Notifications

You’re not receiving notifications from this thread.

Rich Text Blog Posts with ActionText Discussion

Hi Chris, thanks for the video. I was wondering how do I truncate the text on the index.html page? I only want to show 130 characters of the content (previously body).

<%= truncate(blog_post.content, :length 130) %> no longer works.

Thanks

Reply

Maybe it's length:

Reply

Hi Greg, thanks for that. Once updated, that throws up a 'NoMethodError in BlogPosts#index' error.

Reply

After a bit more playing around, managed to solve it. Thanks again @Greg S.

Reply

you can do
<%= truncate(blog_post.content.to_plain_text, length: 130) %>

Reply

In my case, I can upload images but they fail afterwards. Something about vips

Reply

You're probably running into the issue listed in
edgerails

from the site -
If there's an attached resource within content field, it might not show properly unless you have libvips/libvips42 package installed locally on your machine. Check their install docs on how to get it."

Reply

To make that editor work in any way shape, or form I had to add following line to the head in the application.html.erb

<script type="text/javascript" src="https://unpkg.com/trix@2.0.0/dist/trix.umd.min.js"></script>
Reply

Thanks! Fixed it for me.

Reply

Helped for me too, but IDK why we should import that tag if it is loaded into js app. 0.0

Reply

To make "content" field to look good in the form without deleting "@import 'actiontext.css';" line from ../stylesheets/application.tailwind.css, you can just run this command in your rails project root directory:
rails tailwindcss:build

This command should fix update styles correctly. And if you plan to make any more changes to styles or styles files, you can run:
rails tailwindcss:watch
to update any styles changes automatically.

Hope this helps

Reply

Chris does not cover this in the video but I thought it was neat. If you want to see your video attachments actually play and not just have an image preview, you just need to modify a couple of files.
app/views/active_storage/blobs/_blob.html.erb

# change this
<% if blob.representable? %>
# to
<% if blob.video? %>
  <video controls="true" width="100%" preload="metadata">
    <source src="<%= rails_blob_url(blob) %>" type="<%= blob.content_type %>"></source>
  </video>
<% elsif blob.representable? %>

AND
config/application.rb

# add this to inside the class
config.after_initialize do
  ActionText::ContentHelper.allowed_attributes.add "style"
  ActionText::ContentHelper.allowed_attributes.add "controls"

  ActionText::ContentHelper.allowed_tags.add "video"
  ActionText::ContentHelper.allowed_tags.add "audio"
  ActionText::ContentHelper.allowed_tags.add "source"
end
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 84,387+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.