我做了多次上傳與回形針基於這個應用程序 https://github.com/websymphony/Rails3-Paperclip-Uploadify它的工作正常,但現在我需要建立一個表單,用戶可以刪除一些這種上傳。創建一個表格,刪除屬於產品的上傳
我有這樣的形式用於刪除上傳一個產品
<%= form_for @product, :html => { :method => :put }, :html => { :multipart => true } do |f| %>
<h3 id="photos_count"><%= pluralize(@product.uploads.size, "Photo")%></h3>
<%= f.fields_for :uploads do |photo_fields| %>
<% if !photo_fields.object.nil? and !photo_fields.object.new_record? %>
<%= image_tag(photo_fields.object.photo.url(:thumb)) %>
<p><%= photo_fields.check_box :_destroy, :class=>'checkbox' %> Delete item</p>
<% end %>
<% end %>
<% end %>
這種形式以某種方式顯示正確的上傳數,我可以在與PRODUCT_ID = product.id所有上傳中找到的終端窗口看到數據庫中,但我沒有在頁面上顯示圖像,你可以在圖像上看到:
終端
Started GET "/admin/products/40" for 127.0.0.1 at Tue Apr 10 10:20:06 +0300 2012
Processing by Admin::ProductsController#show as HTML
Parameters: {"id"=>"40"}
Creating scope :page. Overwriting existing method AdminUser.page.
AdminUser Load (2.4ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 5 LIMIT 1
Creating scope :page. Overwriting existing method Product.page.
Product Load (0.5ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 40 LIMIT 1
Creating scope :page. Overwriting existing method Upload.page.
Upload Load (0.4ms) SELECT `uploads`.* FROM `uploads` WHERE `uploads`.`product_id` = 40 AND (file_avatar = 1) LIMIT 1
Creating scope :page. Overwriting existing method Category.page.
Category Load (0.4ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1
(0.6ms) SELECT COUNT(*) FROM `uploads` WHERE `uploads`.`product_id` = 40
Rendered admin/products/_delete_photos.html.erb (154.3ms)
CACHE (0.0ms) SELECT COUNT(*) FROM `uploads` WHERE `uploads`.`product_id` = 40
附加信息:
upload.rb
class Upload < ActiveRecord::Base
attr_accessible :product_id, :photo
belongs_to :product
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }
attr_accessible :created_at, :photo_file_size, :photo_file_name, :updated_at, :photo_content_type,
:file_avatar, :photo_updated_at, :uploads_attributes, :photo_attributes
end
product.rb
class Product < ActiveRecord::Base
has_many :uploads, :dependent => :destroy
accepts_nested_attributes_for :uploads, :allow_destroy => true
end
uploads_controller.rb
# GET /uploads/1/edit
def edit
@upload = Upload.find(params[:id])
end
# POST /uploads
# POST /uploads.xml
# PUT /uploads/1
# PUT /uploads/1.xml
def update
@upload = Upload.find(params[:id])
respond_to do |format|
if @upload.update_attributes(params[:upload])
format.html { redirect_to(@upload, :notice => 'Upload was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @upload.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /uploads/1
# DELETE /uploads/1.xml
def destroy
@upload = Upload.find(params[:id])
@upload.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
所以這裏的問題是:爲什麼圖像不顯示旁邊的複選框。謝謝。
更新
Processing by ProductsController#update as HTML
Parameters: {"commit"=>"Delete sellected photos", "authenticity_token"=>"af5+YybkZ1KMu/DmZmlui9frqQX5ka8v/vvv0hi9PVo=", "id"=>"42", "product"=>{"uploads_attributes"=>{"7"=>{"id"=>"322", "_destroy"=>"1"}, "6"=>{"id"=>"321", "_destroy"=>"0"}, "5"=>{"id"=>"320", "_destroy"=>"0"}, "4"=>{"id"=>"319", "_destroy"=>"0"}, "3"=>{"id"=>"318", "_destroy"=>"0"}, "2"=>{"id"=>"317", "_destroy"=>"0"}, "1"=>{"id"=>"316", "_destroy"=>"0"}, "0"=>{"id"=>"315", "_destroy"=>"0"}}}, "utf8"=>"✓"}
後,傑西的答覆我現在已經在我的網頁圖像,與複選框,但我查了一些人後,按下提交按鈕的照片不會被刪除
products_controller
DEF更新 @product = Product.find(PARAMS [:ID])
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to(@product, :notice => 'Product was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
末
問題的部分解決了,謝謝,你能否看看問題的更新部分,因爲刪除上傳仍然存在一個小問題。 – rmagnum2002 2012-04-10 14:37:08
爲什麼你不註釋掉attr_accessible,看看是否修復了它......如果是這樣,那麼它與attr_accessible有關,敲除destroy屬性。 (也許添加_destroy到列表) – 2012-04-10 15:08:58
我試圖刪除attr_accessible,它沒有這樣做..關於添加_destroy到列表中,我不知道你的意思。 – rmagnum2002 2012-04-10 15:12:43