创建用于删除属于产品的上传的表单
我使用基于此应用程序https://github.com/websymphony/Rails3-Paperclip-Uploadify的回形针进行了多次上传,并且它工作正常,但现在我需要构建一个表单,用户可以删除部分上传内容。
我有这个表格来删除一个产品的上传
{ :method => :put }, :html => { :multipart => true } do |f| %> 'checkbox' %> Delete item
这个表单以某种方式显示正确的上传号码,我可以在终端窗口中看到所有上传产品都带有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 { :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 :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"=>"✓"}
在Jesse的回复之后,我现在有了我的页面中的图像,带有复选框,但是在我检查了一些图片并按下提交按钮后,照片不会被删除
products_controller
def update @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
结束
您正在尝试使用nested_attributes,但尚未在Product模型上启用它
class Product < ActiveRecord::Base has_many :uploads, dependent: :destroy, autosave: true accepts_nested_attributes_for :uploads, allow_destroy: true, reject_if: :all_blank end
更多: http : //apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for