我有Post模型:
class Post < ApplicationRecord
belongs_to :user
has_rich_text :content
end在Post#index上,我想显示一个带有标题、图像和描述的帖子。对于图像和描述,我如何从ActionText获取它们?
我认为获取第一个图像~获取content_type为图像的第一个活动存储blob (属于邮报)。
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
发布于 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
@post = Post.with_rich_text_text_and_embeds.find(params[:id])show.html.erb
<% if !post.text.embeds.empty? %>
<%= image_tag post.text.embeds.find{|embeds| embeds.image?}.variant(resize_to_limit: [200, 200]) %>
<% end %>将欣赏任何更有效的解决方案。
发布于 2019-10-19 21:34:20
ActionText模型提供了关联embeds,因此您可以使用方便的方法image?对其调用find
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-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
https://stackoverflow.com/questions/57633659
复制相似问题