【问题标题】:Why I need to select image again after validation failed为什么验证失败后我需要再次选择图像
【发布时间】:2017-10-26 05:41:50
【问题描述】:

我有两个模型

class ComfortFactorSubCategory < ApplicationRecord
   has_one  :image, as: :imageable, dependent: :destroy
   validates :heading, presence: true
   accepts_nested_attributes_for :image, :reject_if => lambda { |a| a[:name].blank? }, allow_destroy: true
end

class Image < ApplicationRecord
  belongs_to :imageable, polymorphic: true
  mount_uploader :name, IconUploader
end

我的管理员/comfort_factor_sub_category.rb 我有这些行

ActiveAdmin.register ComfortFactorSubCategory do
  permit_params do
    permitted = [:heading, image_attributes: [:name, :_destroy, :id], additional_instruction_ids: []]
  permitted
  end
  form do |f|
    f.inputs do
      f.input :heading
      f.input :additional_instructions, as: :select, collection: AdditionalInstruction.pluck(:description, :id)
      f.fields_for :image do |b|
        b.input :name, label: "Image", :as => :file
      end
    end
    f.actions
  end
end

当我提交带有错误信息的表单时,可以说没有标题并且验证失败,为什么我在第一次提交时确实需要再次选择图像

【问题讨论】:

    标签: ruby-on-rails activerecord activeadmin carrierwave polymorphic-associations


    【解决方案1】:

    看来你需要f.hidden_filed :name_cache 来缓存上传的文件

    查看 CarrierWave making uploads work across form redisplays 部分和 upload through accept_nested_attributes_for wiki 文章

    【讨论】:

      【解决方案2】:

      出于安全原因,这受到浏览器的限制。它避免了在呈现错误页面时使用默认文件对象的值预填充表单。

      这是一个关于这个的detailed excerpt

      但是,浏览器通常不支持此功能。通常 解释是“安全原因”。事实上,这将是一种安全 如果用户磁盘中的文件在没有用户授权的情况下提交,则存在风险 内容。引诱一些用户提交可能太容易了 一些密码文件!

      我还发现这个another answer 在谈论与 ASP.net 框架相同的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多