13

我想为以下表单创建一个编辑页面。问题是,当用户浏览到编辑页面时,brand_name 和 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 %>
4

3 回答 3

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

然后你可以做

<%= f.input :avatar, :as => :avatar %>
于 2012-05-25T02:16:41.653 回答
6

这就是我工作所需要的(在haml中):

=simple_form_for @user, :html => {:multipart => true } do |f|
  =f.file_field :image
于 2013-06-05T00:25:58.350 回答
0

来自回形针的 github 页面的新/编辑视图的代码如下所示:

<%= form_for :user, @user, :url => user_path, :html => { :multipart => true } do |form| %>
  <%= form.file_field :avatar %>
<% end %>

所以也许你也应该尝试m.file_field包括在内:html => { :multipart => true }?虽然我个人更喜欢Attachment-Fu

于 2012-05-25T02:12:04.667 回答