【问题标题】:Rails Paperclip inserting instead of updatingRails 回形针插入而不是更新
【发布时间】:2014-02-18 15:10:24
【问题描述】:

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

我创建了一个用于创建/编辑产品的 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 %>

app/inputs/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#update

  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

【问题讨论】:

  • 也许在nested_attributes 上添加一个条件,如果属性为空则忽略?视图中显示的图像是否至少来自您的image_fields 部分?
  • 嘿!这似乎解决了它。我添加了: :reject_if => :all_blank.
  • 根据我下面的回答,这解决了问题,但会产生破坏问题。

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


【解决方案1】:

对于后代,@omarvelous 的评论似乎已经解决了它:

如果全部空白,我将拒绝添加到我的accepts_nested_attributes_for

accepts_nested_attributes_for :images, :allow_destroy => true, :reject_if => :all_blank

编辑: 事实证明,此解决方案存在问题。当 reject_if => :all_blank is 时,它会阻止破坏正常工作。请参阅this post,它显示了一种解决方法——我没有设法开始工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2021-07-19
    相关资源
    最近更新 更多