Ramona
Ramona

Reputation: 21

Rails 8 "block in create" when I try to upload an image using action_text. How do I debug this?

I'm exploring Rails 8 action_text. Using Windows 11(new install) with Ubuntu WSL. I've followed this tutorial on the RoR website for creating a basic blog but I'm getting an error. The rich text saves though but the image doesn't save. Not sure if it's Ubuntu (I'm not good at Linux and using Linux remotely from Windows 11) or permissions or what.

I get a 'block in create' in the console when I try to upload a local image stored in dummyapp/dummy_files. I don't know how to find a way as to why there's a block which is why it's not saving the image.

I've tried Outputting to rails console using @post.errors.messages which shows no errors.

post.rb

class Post < ApplicationRecord
has_rich_text :body

end

posts_controller.rb

def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: "Post was successfully created." }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
    
 
private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params.expect(:id))
    end

    # Only allow a list of trusted parameters through.
    def post_params
      params.expect(post: [ :title, :body ])
    end

posts/_form.rb

<%= form_with(model: post) do |form| %>
  <% if post.errors.any? %>
    <div style="color: red">
    `enter code here`<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
      <ul>
        <% post.errors.each do |error| %>
          <li><%= error.full_message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div>
    <%= form.label :title, style: "display: block" %>
    <%= form.text_field :title %>
  </div>

  <div>
    <%= form.label :body, style: "display: block" %>
    <%= form.rich_textarea :body %>
  </div>

  <div>
    <%= form.submit %>
  </div>
<% end %>

console output

Started POST "/posts" for ::1 at 2024-11-28 19:23:07 -0800
Processing by PostsController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"title"=>"Suck an egg!", "body"=>"<div></div>"}, "commit"=>"Create Post"}
  TRANSACTION (0.2ms)  BEGIN /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  Post Create (1.3ms)  INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES ('Suck an egg!', NULL, '2024-11-29 03:23:07.667889', '2024-11-29 03:23:07.667889') RETURNING "id" /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  ActionText::RichText Create (0.4ms)  INSERT INTO "action_text_rich_texts" ("name", "body", "record_type", "record_id", "created_at", "updated_at") VALUES ('body', '<div></div>', 'Post', 17, '2024-11-29 03:23:07.691089', '2024-11-29 03:23:07.691089') RETURNING "id" /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  ActiveStorage::Attachment Load (0.3ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = 16 AND "active_storage_attachments"."record_type" = 'ActionText::RichText' AND "active_storage_attachments"."name" = 'embeds' /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  Post Update (0.3ms)  UPDATE "posts" SET "updated_at" = '2024-11-29 03:23:07.697759' WHERE "posts"."id" = 17 /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
  TRANSACTION (2.5ms)  COMMIT /*action='create',application='Dummyapp',controller='posts'*/
  ↳ app/controllers/posts_controller.rb:28:in `block in create'
Redirected to http://localhost:3000/posts/17
Completed 302 Found in 53ms (ActiveRecord: 4.9ms (4 queries, 0 cached) | GC: 1.0ms)

Any ideas as to why I can’t upload an image?

Upvotes: 0

Views: 76

Answers (1)

Ramona
Ramona

Reputation: 21

Turns out Rails 8 requires both libvips AND Imagemagick.

Re-did this tutorial from RoR site exactly as he does it. This time I got this message from console when trying to create a post with an image:

LoadError (Could not open library 'vips.so.42': vips.so.42: cannot open shared object file: No such file or directory.
Could not open library 'libvips.so.42': libvips.so.42: cannot open shared object file: No such file or directory.

RoR's Active Storage Overview says we require to install libvips v8.6+ or ImageMagick but actually we need BOTH.

Here's the article where I found out

Upvotes: 0

Related Questions