2014-02-18 30 views
1

我創建了一個包含Product和Image模型的簡單應用程序。產品has_many圖像和圖像具有附加的文件屬性(回形針)。Rails回形針插入而不是更新

我創建了一個用於創建/編輯產品的simple_form,它在創建時可以很好地工作。但是,在編輯具有N張圖像的產品時,導軌會插入更多N個文件 - 空文件。

我已經設置了一個簡單窗體自定義輸入,用於測試圖像附件是否存在,而不是呈現構建器輸入,它只呈現image_tag()。

我看到生成的HTML,它顯示出一些奇怪的事情,一個隱藏的標籤:

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

而且在軌服務器控制檯我看到:

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

也許在nested_attributes上添加一個條件,如果屬性爲空則會忽略它?視圖中顯示的圖像是否至少來自「image_fields」部分? – omarvelous

+0

嘿!這似乎解決了它。我添加了::reject_if =>:all_blank。 –

+0

按照我下面的回答,這可以解決這個問題,但會造成銷燬問題。 –

回答

0

對於後人,@omarvelous'評論似乎已經解決了這個問題:

我加入了拒絕,如果所有空白我accepts_nested_attributes_for

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

編輯: 原來有一個問題,這個解決方案。當reject_if =>:all_blank時,它會阻止破壞正常工作。請參閱this post,其中顯示了一種解決方法 - 我無法開始工作。