我有一个使用回形针绑定用户及其头像的应用程序,我在编辑用户个人资料时遇到问题,因为上传图像的按钮显示"您没有选择任何文件&#34 ;在这种情况下,如果我通过text_field更改file_field类型,那么我有文件的url,如何在标签中自动恢复?
contact.haml
-# Profile picture
= form.label :image, t("settings.profile.profile_picture")
#img_preview
= image_tag(@current_user.image.url(:thumb), id: "user_avatar") if @current_user.image
= form.file_field :image, :size => 30, :id => "avatar_file"
person.rb
has_attached_file :image, :styles => {
:medium => "288x288#",
:small => "108x108#",
:thumb => "48x48#",
:original => "600x800>"}
process_in_background :image
#validates_attachment_presence :image
validates_attachment_size :image, :less_than => 9.megabytes
validates_attachment_content_type :image,
:content_type => ["image/jpeg", "image/png", "image/gif",
"image/pjpeg", "image/x-png"] #the two last types are sent by IE.
答案 0 :(得分:1)
回答V2:
出于安全原因,您似乎无法直接为文件字段分配值;因此,最简单的方法是根据图像的存在调整标签。
所以,我认为你不想要的消息来自t("settings.profile.profile_picture")
。要解决此问题,请
-# Profile picture
- if @current_user.image.present?
= form.label :image, @current_user.image.url(:thumb) # Or whatever text you want
- else
= form.label :image, t("settings.profile.profile_picture")
#img_preview
= image_tag(@current_user.image.url(:thumb), id: "user_avatar") if @current_user.image
= form.file_field :image, size: 30, id: "avatar_file", value: @current_user.image&.attachment&.url
这看起来像你在这里寻找的那样吗?