问题回形针与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中查看弹出窗口(这是部分)
Nuevo producto estrella <% remote_form_for @product, :url => {:controller => "products", :action => "create_starred_product"}, :html => {:method => :post, :id => 'uploadForm', :multipart => true} do |f| %> <%= f.text_field :name, :class => "textarea" %>
<%= f.text_field :price, :class => "textarea" %>
<%#= f.file_field :photo %> <% count = 0 %> <%# f.fields_for "product_images_attributes[]", ProductImage.new do |builder| %> <% f.fields_for :product_images, 4.times {ProductImage.new} do |builder| %> <% if builder.object.new_record? %> <%= builder.file_field :photo, :class => "file-upload" -%>
<% end %> <%#= builder.text_field :testfield %> <% end %>
<%= f.text_area :description, :rows => 10, :class => "textarea clearfix" -%>
<%#= f.submit "Crear" %> <%= link_to "Crear", "#", :id => "product_submit" %>
<% end %>
我正在使用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 – 强烈推荐。
- Rails中的AJAX:在完成模型#update后显示模型#new form
- 为什么这个button_to在Rails 3.2.11中呈现错误?
- Rails:用户使用以下命令销毁不相关的对象后退出:remote => true
- 使用Rails,jquery和best_in_place gem进行就地编辑
- Rails AJAX评论清楚textarea
- Rails 2到Rails 3:使用link_to而不是link_to_remote(包括远程和更新)
- 如何使用rails remote:true参数和JSON?
- 如何在Rails 3中使用AJAX请求实现重定向响应?
- 在Ruby on Rails中旋转图像,类似于AJAX