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

I don't know what problem was for you, but fixed mine too! I believe I ran the same steps as the video, but the rich text field wasn't being rendered, this fixed for me, thanks

Reply

Yeah, without it, the Content rich text area is actually empty for me. and you can't do anything. The Rich text editor is not rendereed at all. I am using Ubuntu environment.
So, the script actually fixs it. It seems that the install command from rails is not doing the correct job (including the misconfigured css).
Thanks Patryk!

Reply

If you're using import maps, you can adjust the pin, instead:


pin "trix", to: "https://ga.jspm.io/npm:trix@2.1.10/dist/trix.esm.min.js"

With the bare pin "trix", I was getting a much earlier version of Trix.

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

When I add "rich" to form.text_area, the content box is no longer active. i cant type into it, i cant tab into it. its just a visual piece. "bold, italics, etc" do not appear. it does not seem to work.

Reply

When uploading to Render I receive the error of "ActionView::Template::Error (The asset "actiontext.css" is not present in the asset pipeline." Note: It works in the development environment.

Reply

In case this happens to anyone else, I discovered the issue. For some reason the actiontext.css file was categorized as an "Untracked file" per git. I had to manually add the file to stage for commit (i.e., git add <file>). Additionally, it looks like additional Action Text and Action Storage files were all categorized as untracked (e.g., db migrations). These needed to be manually staged as well to ensure Action Text works.

Reply

If anyone using HAML, and having the problem where the content rich_text_field is of type="hidden"

This fixes it. (Thanks Patryk)

!!!
%html
  %head
    %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
    %title BlogV1
    %meta{:content => "width=device-width,initial-scale=1", :name => "viewport"}/
    = csrf_meta_tags
    = csp_meta_tag
    = stylesheet_link_tag "tailwind", "data-turbo-track": "reload"
    = javascript_importmap_tags
  %script{type: "text/javascript", src: "https://unpkg.com/trix@2.0.0/dist/trix.umd.min.js"}
  %link{rel: "stylesheet", href: "https://unpkg.com/trix@2.0.0/dist/trix.css"}
    = stylesheet_link_tag "application", media: "all"
    = javascript_include_tag "application"
  %body.prose.mx-auto.border
    = render "layouts/flash"
Reply
Join the discussion
Create an account Log in

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

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

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