问题回形针与ajax

我正在阅读本教程: http : //sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/因为我需要在独立模型中保存产品图像。 但是,当Product.create执行时,请不要保存产品图像模型的数据。

注意:我使用新的包操作,因为我需要使用ajax来创建新产品。

我需要帮助。

我的代码

控制器

class Admin::PacksController  @pack } end end def create_starred_product product = Product.new(params[:product]) product.save ... end 

视图

  {:controller => "products", :action => "create_starred_product"}, :html => {:multipart => true} do |f| %> 
....#OTHER FIELDS OF PRODUCT. ITS SAVE METHOD IS OK
Imágenes
"textarea" -%>

楷模

 class Product  :pack_products has_many :product_images, :dependent => :destroy #PAPERCLIP accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].blank? } end class ProductImage  "/:attachment/:class/:id/:style_:basename.:extension", :styles => { :medium => "300x300>", :thumb => "100x100>", :small => "30x30>" } end 

就像你说的那样,你无法使用AJAX上传文件。 但是,有其他解决方案。

实现此目的的常用技术是使用iframe。 看看这里 ,本教程面向attachment_fu,但它也适用于paperclip。 Uploadify值得一试,也可以在rails 2.3和rails 3上使用paperclip。

我和我的伙伴一起找到了解决方案。

我的错误是在嵌套属性表单中,在Ajax中没有。所以我找到了使用paperclip和Ajax的解决方案。 现在我发布了示例代码。

但是我解释了代码。

楷模

两种型号

产品和Product_images

当我以ajaxforms创建产品时,我想要这样(每个产品belongs_to pack)。 每个产品在product_images模型中都有很多图像。 因此我也想保存图像。

代码

控制器Ajax动作

 class ProductsController < ApplicationController def create_starred_product product = Product.new(params[:product]) product.save render :update do |page| page.replace_html 'star-product', :partial => "admin/products/star_product_name", :locals => {:star_product => product} page.call "$j.modal.close" end end end 

楷模

产品型号

 class Product < ActiveRecord::Base has_many :packs, :through => :pack_products has_many :product_images, :dependent => :destroy #PAPERCLIP accepts_nested_attributes_for :product_images#, :reject_if => lambda { |t| t['product_image'].blank? } has_attached_file :photo, :url => "/:attachment/:class/:id/:style_:basename.:extension", :styles => { :medium => "300x300>", :thumb => "100x100>", :small => "30x30>" } 

Product_images模型

 class ProductImage < ActiveRecord::Base belongs_to :product has_attached_file :photo, :url => "/:attachment/:class/:id/:style_:basename.:extension", :styles => { :medium => "300x300>", :thumb => "100x100>", :small => "30x30>" } end end 

在ajax中查看弹出窗口(这是部分)

   

我正在使用jquery和jquery-form插件(在谷歌搜索)

您可以在https://github.com/apneadiving/Pic-upload—Crop-in-Ajax上找到两个版本的ajax上传。

分支master :它使用http://aquantum-demo.appspot.com/file-upload

分支uploadify :它使用Uploadify: http : uploadify

两者都有内置裁剪。

问题是嵌套属性form.later我发布了代码

另外一个帮我解决这个问题的gem我刚刚遇到同样的问题就是remotipart – 强烈推荐。