如何解决rails file_field未显示
我想让我的应用使用Shrine上传多个文件,但是one doc建议两个file_field
,而other建议一个。在他们的讨论论坛上发布问题后,建议我隐藏一个名为files[]
的问题。无论我是否执行此操作,第一个file_field
都始终无法呈现。为什么此字段不显示?
<%= 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 %>
型号:
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
控制器:
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
解决方法
仔细阅读第一个链接:它表示单个字段(i.file_field :image
)用于显示现有图像(这就是示例中将其包装在f.fields_for :photos
中的原因)和多个字段({ {1}})用于上传新文件。因此,如果您的file_field_tag "files[]",multiple: true
没有@item
,则不会显示该字段。
让我知道是否需要进一步澄清-非常乐意提供帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。