加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在multipart simple_form中更新paperclip头像

发布时间:2020-12-16 20:00:45 所属栏目:百科 来源:网络整理
导读:我想为以下表单创建一个编辑页面.问题是当用户浏览到编辑页面时,品牌名称和名称是预先填充的,但即使在“样式”中存在头像时,图像上传字段也显示“没有选择文件”.请让我知道,如果有办法补救这个.谢谢! 我的编辑形式: %= simple_form_for @style,:html = {
我想为以下表单创建一个编辑页面.问题是当用户浏览到编辑页面时,品牌名称和名称是预先填充的,但即使在“样式”中存在头像时,图像上传字段也显示“没有选择文件”.请让我知道,如果有办法补救这个.谢谢!

我的编辑形式:

<%= simple_form_for @style,:html => { :class => 'form-horizontal' },:remote => true do |m| %>

    <%= m.input :brand_name,:label => 'Brand',:placeholder => 'Brand' %>    
    <%= m.input :name,:label => 'Style',:placeholder => 'Style' %>
    <%= m.input :avatar,:label => "Image" %>

    <div class="form-actions" style = "background:none">
      <%= m.submit nil,:class => 'btn btn-primary' %>
      <%= link_to 'Cancel',styles_path,:class => 'btn' %>
    </div>

<% end %>

解决方法

昨天刚刚实施.在/ app / inputs中自定义输入
class AvatarInput < SimpleForm::Inputs::FileInput
def input
out = '' # the output string we're going to build
# check if there's an uploaded file (eg: edit mode or form not saved)
if object.send("#{attribute_name}?")
  # append preview image to output
  # <%= image_tag @user.avatar.url(:thumb),:class => 'thumbnail',id: 'avatar' %>
  out << template.image_tag(object.send(attribute_name).url(:thumb),id: 'avatar')
  end
# append file input. it will work accordingly with your simple_form wrappers
    (out << @builder.file_field(attribute_name,input_html_options)).html_safe
  end
end

那你可以做

<%= f.input :avatar,:as => :avatar %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读