根据我添加action_text/trix到我的 rails 应用程序的文档。当我更新内容时。它向我显示了以下错误。任何想法?

Showing /home/dell/code/tm/app/views/active_storage/blobs/_blob.html.erb,ruby-on-rails,ruby,trix,actiontext"/>
	














首页 > 解决方案 > 无法将图像解析为 URL:# 的未定义方法“to_model”

根据我添加action_text/trix到我的 rails 应用程序的文档。当我更新内容时。它向我显示了以下错误。任何想法?

Showing /home/dell/code/tm/app/views/active_storage/blobs/_blob.html.erb

问题描述

根据我添加action_text/trix到我的 rails 应用程序的文档。当我更新内容时。它向我显示了以下错误。任何想法?

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

宝石文件:

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' 

我的模型:

class Page < ApplicationRecord

  has_rich_text :body

end

和迁移:

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

      t.timestamps
    end
  end
end

这是视图:

<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>

根据文档,我没有在表格中添加body列。pages任何想法?我错过了什么吗?

更新: 我添加gem 'image_processing'Gemfile. 但结果是一样的:(

更新 2:我也安装imagemagick在我的 Ubuntu 中。

更新 3:而且它不仅在update行动中起作用。


(将所有列表的显示属性设置为内联,或者将其容器设置为 obvi 或 flex)是它们都显示在一行中的原因。

标签: ruby-on-railsrubytrixactiontext

解决方案


您是否运行bin/rails action_text:install并安装了迁移?还有主动存储?

https://guides.rubyonrails.org/action_text_overview.html#installation在那里您可以找到操作方法,它还链接到用于嵌入附件的 activestorage。


推荐阅读