简体   繁体   English

来自多态模型的图像不显示

[英]Image from polymorphic model does't display

I have a model named Hen:我有一个名为 Hen 的模型:

class Hen < ApplicationRecord
  has_many :pictures, as: :imageable
  validates :name, :description, presence: true
end 

I added the :picture to whitelisted params in the HensController as well我也将:picture添加到 HensController 中的白名单参数中

Picture model:图片型号:

class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true
  has_attached_file :image
end

When creating new Hen, I can check that the instance is created.创建新母鸡时,我可以检查实例是否已创建。 In console Hen.last.pictures returns在控制台Hen.last.pictures返回

[["imageable_id", 3], ["imageable_type", "Hen"], ["LIMIT", 11]] => # [["imageable_id", 3], ["imageable_type", "Hen"], ["LIMIT", 11]] => #

,so I assume that it is fine there. ,所以我认为那里很好。 ... but I do not know how to dispay it in the view section. ...但我不知道如何在视图部分显示它。 After some tries I have done that:经过一些尝试,我做到了:

<p><%= @hen.pictures do |pic|  %></p>
  <%= image_tag pic.url %>
<% end %>

It dispays它显示

#<Picture::ActiveRecord_Associations_CollectionProxy:0x00005640044034f0>* 

because I left the "=" there (to check if there is any object inside), but the picture is not displayed.因为我把“=”留在了那里(检查里面是否有任何物体),但没有显示图片。 I checked some other variants like pic.image_url and nothing works for me.我检查了一些其他变体,如pic.image_url ,但对我没有任何作用。 Other images on the page display without problems.页面上的其他图像显示没有问题。 Console returns status 200:控制台返回状态 200:

Processing by HensController#show as HTML
  Parameters: {"id"=>"3"}
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Hen Load (0.5ms)  SELECT "hens".* FROM "hens" WHERE "hens"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  ↳ app/controllers/hens_controller.rb:16:in `show'
  Rendering hens/show.html.erb within layouts/application
  Rendered hens/show.html.erb within layouts/application (Duration: 5.9ms | Allocations: 3302)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 125ms (Views: 67.7ms | ActiveRecord: 10.7ms | Allocations: 36995)*

You're not actually looping through pictures.你实际上并不是在循环浏览图片。 You need to use @hen.picutres.each or for picture in @hen.pictures .您需要使用@hen.picutres.each@hen.picutres.each for picture in @hen.pictures Without this, you're just passing a block to @hen.pictures ;没有这个,你只是将一个块传递给@hen.pictures the block gets ignored and the expression returns @hen.pictures ( Picture::ActiveRecord_Associations_CollectionProxy ).该块被忽略,表达式返回@hen.pictures ( Picture::ActiveRecord_Associations_CollectionProxy )。

The equivalent plain-ruby would be等效的普通红宝石将是

@hen.pictures { ... this block isn't used but it's not a syntax error ... }

instead of代替

@hen.pictures.each { |picture| ... this block is used by the `each` method }

Here <%= @hen.pictures do |pic|%> you are printing the picture url.这里 <%= @hen.pictures do |pic|%> 您正在打印图片网址。 you should not include = when you are looping <%= @hen %> wrong you should use <% @hen %> then you need to loop the pictures using each.你不应该包括=当你循环<%= @hen %>错误你应该使用<% @hen %>然后你需要使用每个循环图片。

<% @hen.pictures.each do |pic| %>
  <%= image_tag pic.url %>
<% end %>

if the picture url in hash try to convert it into string using .to_s如果哈希中的图片 url 尝试使用.to_s将其转换为字符串

<%= image_tag pic.url.to_s %>

This may not be the direct answer for the problem, but I found that better option is to use ActiveStorage than creating Polymorphic associations between models.这可能不是问题的直接答案,但我发现使用 ActiveStorage 比在模型之间创建多态关联更好。

First, install the ActiveStorage:首先,安装 ActiveStorage:

rails active_storage:install

then add macro has_one_attached or has_many_attached to the models:然后将宏has_one_attachedhas_many_attached添加到模型中:

class Hen < ApplicationRecord
  has_one_attached :image
  validates :name, :description, presence: true
end 

add to the view form:添加到视图表单:

<%= f.file_field :image %>

whitelist the :image attribute::image属性列入白名单:

def hens_params
  params.require(:hen).permit(:name, :description, :image)                                                           
end

Done.完毕。

To dispay the image in views:要在视图中显示图像:

<%= image_tag hen.image if hen.image.attached? %>

No need to create new model or to nest attributes.无需创建新模型或嵌套属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails无法正确生成多态模型 - Rails does not properly generate polymorphic model Rails模型不保存多态数据 - Rails Model doesn't save data with polymorphic 无法在多态关联上保存嵌套模型 - Can't save nested model on polymorphic association rails-3,来自多态关联的新评论表单,接受表单提交但不显示提交的数据 - rails-3, new comment form from a polymorphic association, accepts form submission but doesn't display submited data 从多态评论模型中找到评论的回复者 - Finding the repliers to a comment from a polymorphic comments model ActiveRecord 从多态关联中获取共享 model - ActiveRecord grab shared model from polymorphic association 从一个模型中检索Rails中的多态关联 - Retrieve polymorphic associations in Rails from one model 基于多态模型属性使用回形针创建不同样式的图像附件? - create different styled image attachments with paperclip based on polymorphic model attribute? 多态图像模型无法通过载波将多幅图像嵌入蒙古语中 - Unable to embed multiple images in mongoid with carrierwave with polymorphic image model Rails如何填充多态关联的“ model_type”字段? - How does Rails populate the “model_type” field for polymorphic associations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM