2

我有帖子模型:

class Post < ApplicationRecord
  belongs_to :user
  has_rich_text :content
end

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

我认为获取第一个图像〜获取第一个活动存储 blob(属于帖子),其 content_type 是图像。

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

参考:关于描述,我看到了这个问题

4

3 回答 3

4

感谢罗兰的回答,应该可以接受。

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

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

post_controller.rb

@post = Post.with_rich_text_text_and_embeds.find(params[:id])

显示.html.erb

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

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

于 2020-03-10T11:07:29.120 回答
2

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

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
于 2019-10-19T13:34:20.133 回答
0

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

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

于 2019-08-24T02:39:21.073 回答