首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 6 ActionText获取第一张图像

Rails 6 ActionText获取第一张图像
EN

Stack Overflow用户
提问于 2019-08-24 06:30:27
回答 3查看 863关注 0票数 2

我有Post模型:

代码语言:javascript
复制
class Post < ApplicationRecord
  belongs_to :user
  has_rich_text :content
end

Post#index上,我想显示一个带有标题、图像和描述的帖子。对于图像和描述,我如何从ActionText获取它们?

我认为获取第一个图像~获取content_type为图像的第一个活动存储blob (属于邮报)。

代码语言:javascript
复制
irb(main):017:0> Post.last.content
  Post Load (22.6ms)  SELECT `posts`.* FROM `posts` ORDER BY `posts`.`id` DESC LIMIT 1
  ActionText::RichText Load (3.2ms)  SELECT `action_text_rich_texts`.* FROM `action_text_rich_texts` WHERE `action_text_rich_texts`.`record_id` = 12 AND `action_text_rich_texts`.`record_type` = 'Post' AND `action_text_rich_texts`.`name` = 'content' LIMIT 1
Traceback (most recent call last):
        1: from (irb):17
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 4.0ms | Allocations: 54)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.6ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.8ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.3ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/content/_layout.html.erb (Duration: 445.6ms | Allocations: 54464)
NoMethodError (undefined method `first' for #<ActionText::RichText:0x00007fb0a9e33620>)
irb(main):018:0> Post.last.content.body
  Post Load (1.6ms)  SELECT `posts`.* FROM `posts` ORDER BY `posts`.`id` DESC LIMIT 1
  ActionText::RichText Load (3.4ms)  SELECT `action_text_rich_texts`.* FROM `action_text_rich_texts` WHERE `action_text_rich_texts`.`record_id` = 12 AND `action_text_rich_texts`.`record_type` = 'Post' AND `action_text_rich_texts`.`name` = 'content' LIMIT 1
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 54)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.3ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.2ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.7ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/attachables/_remote_image.html.erb (Duration: 0.1ms | Allocations: 52)
  Rendered /Users/stronglong/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/actiontext-6.0.0.rc2/app/views/action_text/content/_layout.html.erb (Duration: 115.7ms | Allocations: 53442)
=> #<ActionText::Content "<div class=\"trix-conte...">

Ref:有关说明,请参阅此question

EN

回答 3

Stack Overflow用户

发布于 2020-03-10 19:07:29

感谢Roland的回答,它应该被接受。

要避免N+1查询,请添加with_rich_text_ATTRIBUTE_and_embeds。https://edgeguides.rubyonrails.org/action_text_overview.html#avoid-n-1-queries

我的模型中的内容在text属性中。

posts_controller.rb

代码语言:javascript
复制
@post = Post.with_rich_text_text_and_embeds.find(params[:id])

show.html.erb

代码语言:javascript
复制
<% if !post.text.embeds.empty? %>
    <%= image_tag post.text.embeds.find{|embeds| embeds.image?}.variant(resize_to_limit: [200, 200]) %>
<% end %>

将欣赏任何更有效的解决方案。

票数 4
EN

Stack Overflow用户

发布于 2019-10-19 21:34:20

ActionText模型提供了关联embeds,因此您可以使用方便的方法image?对其调用find

代码语言:javascript
复制
Post.last
    .with_rich_text_text_and_embeds # to avoid N+1 queries
    .content
    .embeds
    .find{|embeds| embeds.image?} # to make sure embed is an image
票数 2
EN

Stack Overflow用户

发布于 2019-08-24 10:39:21

您可以调用content.to_plain_text来获取文本,调用content.embeds来获取所有附件(使用关联来获取第一张图像,因为它可以嵌入其他内容)

https://github.com/rails/rails/blob/master/actiontext/app/models/action_text/rich_text.rb

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57633659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档