Ruby on rails Rails 6 ActionText获取第一个图像
我有一个后模型: 类PostRuby on rails Rails 6 ActionText获取第一个图像,ruby-on-rails,ruby,ruby-on-rails-6,actiontext,Ruby On Rails,Ruby,Ruby On Rails 6,Actiontext,我有一个后模型: 类Post
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...">
参考:我看到这个描述,你可以调用content.to_plain_text获取文本和内容。embeds获取所有附件播放关联以获取第一个图像,因为它可以嵌入其他内容
ActionText模型提供了关联嵌入,因此您可以使用方便的方法image调用find
感谢罗兰的回答,它应该被接受 要避免N+1查询,请使用\u rich\u text\u[ATTRIBUTE]\u和\u嵌入进行添加。 “我的模型”中的内容位于“文本”属性中 posts\u 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 %>
将非常感谢任何更有效的解决方案。如果有人知道我们如何通过一些连接实现更高效的查询,我将非常感谢这样的回答。
<% if !post.text.embeds.empty? %>
<%= image_tag post.text.embeds.find{|embeds| embeds.image?}.variant(resize_to_limit: [200, 200]) %>
<% end %>