简体   繁体   English

rails file_field 未显示

[英]rails file_field not showing

I would like to make my app upload multiple files with Shrine, but one doc suggests two file_field s whereas the other suggests only one.我想让我的应用程序使用 Shrine 上传多个文件,但一个文档建议使用两个file_field另一个建议只使用一个。 After posting a question to their discourse forum, it was suggested that I hide the one named files[] .在他们的话语论坛上发布问题后,有人建议我隐藏名为files[]那个。 Whether I do this or not, the first file_field always fails to render.无论我是否这样做,第一个file_field总是无法呈现。 Why does this field not display?为什么这个字段不显示?

<%= form_for @item, html: { enctype: "multipart/form-data" } do |f| %>
 <%= f.fields_for :photos do |i| %>
  <%= i.label :image %>
  <%= i.hidden_field :image, value: i.object.cached_photos_data, class: "upload-data" %>
  <%= i.file_field :image, class: "upload-file" %> /// why is this not rendering?😢
 <% end %>
 <%= file_field_tag "files[]", multiple: true %> // what purpose does this one serve?
 
 <%= f.text_field :title %>
      
 <%= f.submit "Submit" %>    
<% end %>

Models:楷模:

class Item < ApplicationRecord
 has_many :photos, as: :imageable, dependent: :destroy
end

class Photo < ApplicationRecord
 include ImagesUploader::Attachment(:image)
 belongs_to :imageable, polymorphic: true
 validates_presence_of :image
end

Controller:控制器:

class ItemsController < ApplicationController
 def new
  @item = current_user.items.new
 end

 def create
  @item = current_user.items.create(item_params)
  @item.save
 end

 private
 def item_params
  params.require(:item).permit(:title, photos_attributes: { image: [] })
 end
end

Read the first link carefully: It says that the single field ( i.file_field :image ) is used to display existing images (which is why it's wrapped in f.fields_for :photos in the example) and the multiple field ( file_field_tag "files[]", multiple: true ) is used to upload new files.仔细阅读第一个链接:它说单个字段( i.file_field :image )用于显示现有图像(这就是它在示例中被包裹在f.fields_for :photos中的原因)和多个字段( file_field_tag "files[]", multiple: true ) 用于上传新文件。 So if your @item doesn't have an :image , then the field isn't shown.因此,如果您的@item没有:image ,则不会显示该字段。

Let me know if this needs any further clarification – happy to help!如果这需要进一步澄清,请告诉我 - 很乐意提供帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM