我是 ActiveStorage 的新手。
我有一个名为 Landing 的模型,带有附件
:image
。 (我正在使用 ActiveStorage)。
创建新记录时一切正常,但当您更新任何属性时,图像将被删除,除非您再次上传附件。 这是为什么呢?如果没有上传新附件,我是否需要明确告诉我的控制器来处理附件逻辑?这个内置没有帮助程序?
在我的表单中,我有以下输入:
<%= f.file_field :image, class: "rounded input w-full my-2" %>
在我的控制器中
def update
if @landing.update(landing_params)
redirect_to admin_landings_path, notice: "Actualizado con éxito :)"
else
render :edit, status: :unprocessable_entity
end
end
我的模型
has_one_attached :image do |attachable|
attachable.variant(:thumb, resize_to_fill: [500, 500])
attachable.variant(:big, resize_to_fill: [1200, 1200])
end
有了 ActiveStorage 和
has_many_attached
关系,我们需要将其添加到生产和开发环境中:
config.active_storage.replace_on_assign_to_many = false
这个方法对我有用。