在multipart simple_form中更新回形针头像

Abr*_*ram 13 ruby-on-rails

我想为下面的表单创建一个编辑页面.问题在于,当用户浏览编辑页面时,brand_name和名称被预先填充,但是图像上载字段显示"没有选择文件",即使存在"样式"的化身也是如此.如果有办法解决这个问题,请告诉我.谢谢!

我的编辑表格:

<%= 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 %>
Run Code Online (Sandbox Code Playgroud)

DVG*_*DVG 19

刚刚实施了这个.在/ 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), :class => 'thumbnail', 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
Run Code Online (Sandbox Code Playgroud)

那你可以做

<%= f.input :avatar, :as => :avatar %>
Run Code Online (Sandbox Code Playgroud)


Esk*_*im0 6

这就是我需要的所有工作(以haml为单位):

=simple_form_for @user, :html => {:multipart => true } do |f|
  =f.file_field :image
Run Code Online (Sandbox Code Playgroud)