Can't resolve image into URL: undefined method `to_model' for #<ActiveStorage::VariantWithRecord
Asked Answered
C

2

8

According to documentation I added action_text/trix to my rails app. And when I update a content. It shows me following errors. Any idea?

Showing /home/dell/code/tm/app/views/active_storage/blobs/_blob.html.erb where line #3 raised:

Can't resolve image into URL: undefined method `to_model' for #<ActiveStorage::VariantWithRecord:0x0000559255e1be40 @blob=#<ActiveStorage::Blob id: 12, key: "n48e43looijem16cdq5d48sqhu4d", filename: "Screenshot from 2021-08-28 17-10-25.png", content_type: "image/png", metadata: {}, service_name: "local", byte_size: 93757, checksum: "up8PwkoYwf+Y6NTRJvbAUA==", created_at: "2021-08-29 12:13:04.912555000 +0800">, @variation=#<ActiveStorage::Variation:0x0000559255e248d8 @transformations={:format=>"png", :resize_to_limit=>[1024, 768]}>>
Did you mean?  to_yaml

ActionView::Template::Error (Can't resolve image into URL: undefined method `to_model' for #<ActiveStorage::VariantWithRecord:0x0000559255e1be40 @blob=#<ActiveStorage::Blob id: 12, key: "n48e43looijem16cdq5d48sqhu4d", filename: "Screenshot from 2021-08-28 17-10-25.png", content_type: "image/png", metadata: {}, service_name: "local", byte_size: 93757, checksum: "up8PwkoYwf+Y6NTRJvbAUA==", created_at: "2021-08-29 12:13:04.912555000 +0800">, @variation=#<ActiveStorage::Variation:0x0000559255e248d8 @transformations={:format=>"png", :resize_to_limit=>[1024, 768]}>>
Did you mean?  to_yaml):
    1: <figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
    2:   <% if blob.representable? %>
    3:     <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
    4:   <% end %>
    5: 
    6:   <figcaption class="attachment__caption">
  
app/views/active_storage/blobs/_blob.html.erb:3

Gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.0.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.3', '>= 6.1.3.2'
# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
gem 'devise'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
# gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
gem 'pagy', '~> 3.5'
gem 'money-rails', '~>1.12'
gem "view_component", require: "view_component/engine"

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

gem 'metka', '~> 2.3.1'
gem 'ransack', '~> 2.4.2'
gem 'country_select', '~> 6.0'
gem 'rails-i18n', '~> 6.0.0' 

My model:

class Page < ApplicationRecord

  has_rich_text :body

end

And migration:

class CreatePages < ActiveRecord::Migration[6.1]
  def change
    create_table :pages do |t|
      t.string :title
      t.string :slug

      t.timestamps
    end
  end
end

Here is the view:

<div class="row">
  <div class="col-md-12">
    <div class="mb-3">
      <%= f.label :body, class: 'form-label required' %>
      <%= f.rich_text_area :body %>
      <%= display_error_messages(@resource, 'body') %>
    </div>
  </div>
</div>

According to documentation I didn't add body column to pages tables. Any idea? Am I miss something?

UPDATE: I addeg gem 'image_processing' to Gemfile. But result is the same :(

UPDATE 2: I also installed imagemagick in my Ubuntu.

UPDATE 3: And it's not work only in update action.

Cathouse answered 29/8, 2021 at 4:42 Comment(1)
Did you manage to find a solution?Maladjustment
V
17

I found a solution.

I was writing a Ruby Engine / Ruby Plugin as a gem, and apparently ActiveStorage was unable to figure out the polymorphic routing thanks to the namespacing.

I had to change "views/active_storage/blobs/_blob.html.erb" as follows:

- <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
+ <%= image_tag main_app.url_for(blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ])) %>

Thanks @rbngzlv for the answer on Github.

Vallation answered 14/6, 2022 at 3:56 Comment(2)
thank you! I was going crazy trying to figure this out, knew it had to do with it being an engine but wasn't sure what. This change was in the main app or from the engine?Discoid
Underrated comment. I was hitting the same issue on Rails 7 and this fixed it for me! Still works in 2024...Wadewadell
I
0

Have you run bin/rails action_text:install and installed the migrations? And also activestorage?

https://guides.rubyonrails.org/action_text_overview.html#installation There you can find how do do it and it also links to activestorage for embdedded attachments.

Improve answered 24/9, 2021 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.