Rails Paperclip插入而不是更新

时间:2014-02-18 05:28:14

标签: ruby-on-rails paperclip simple-form

我创建了一个包含Product和Image模型的简单应用程序。产品has_many图像和图像具有附加文件属性(回形针)。

我创建了一个用于创建/编辑产品的simple_form,它在创建时工作正常。但是,在编辑具有N个图像的产品时,rails会插入更多N个文件 - 空文件。

我设置了一个简单表单自定义输入,用于测试图像附件是否存在,而不是渲染构建器输入,它只呈现image_tag()。

我看到生成的html,它显示了一些奇怪的东西,一个隐藏的标记:

<input id="product_images_attributes_0_id" name="product[images_attributes][0][id]" type="hidden" value="14">

在rails服务器控制台中,我看到:

Processing by ProductsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"asdfasdfaasdf=", "product"=>{"reference"=>"Y1112CYL.E2", "images_attributes"=>{"0"=>{"id"=>"14"}}}, "commit"=>"Update Product", "id"=>"20"}
  Product Load (0.6ms)  SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1  [["id", "20"]]
Unpermitted parameters: id
   (0.2ms)  BEGIN
  SQL (1.0ms)  INSERT INTO "images" ("created_at", "imageable_id", "imageable_type", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["created_at", Tue, 18 Feb 2014 05:05:13 UTC +00:00], ["imageable_id", 20], ["imageable_type", "Product"], ["updated_at", Tue, 18 Feb 2014 05:05:13 UTC +00:00]]
   (0.6ms)  COMMIT

以下是我的实施代码。如果有人可以帮助我会很开心!如果我遗漏了代码的任何导入部分,请原谅我,我很乐意编辑问题以包含它。

_form.html.erb

<%= simple_form_for(@product) do |f| %>
    <%= f.error_notification %>
    <div class="inputs">
    <%= f.input :reference %>
    <h3>Images</h3>
    <div id='images'>
      <%= f.simple_fields_for :images do |image| %>
        <%= render 'image_fields', :f => image %>
      <% end %>
      <div class='links'>
        <%= link_to_add_association 'New image', f, :images %>
      </div>
    </div>
  </div>
  <div class="actions">
    <%= f.button :submit %>
  </div>
<%end%>

_image_fields.html.erb

<%= content_tag :div, class: "nested-fields images-fields" do %>
    <%= content_tag :div, id: "new-image" do %>
        <% if f.object.photo.exists? %>
            <% f.template.image_tag(f.object.photo.url(:thumb)) %>
        <% else %>
            <% f.input :photo, :as => :photo %>
        <% end %>
    <% end %>
<% end %>

应用程序/输入/ photo_input.erb

class PhotoInput < 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: 'photo')
    else
      # 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
end

的ProductsController#更新

  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

1 个答案:

答案 0 :(得分:0)

对于后人来说,@ mayrvelous'评论似乎解决了它:

如果我的accepted_nested_attributes_for

全部为空,我添加了拒绝
accepts_nested_attributes_for :images, :allow_destroy => true, :reject_if => :all_blank

编辑: 事实证明这个解决方案存在问题。当reject_if =&gt; :all_blank是它阻止破坏工作正常。请参阅显示解决方法的this post - 我无法开始工作。