问题回形针使用Ajax回形针、问题、Ajax

2023-09-10 19:01:38 作者:南笙北执

我阅读本教程: HTTP ://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/  因为我需要保存的产品图片在独立的模型。然而,当Product.create执行鸵鸟政策保存的产品图片模型的数据。

注:我用的是新包装的动作,因为我需要使用Ajax为创建新产品

请我需要帮助。

我的code

控制器

 类管理:: PacksController<的ApplicationController
      高清新
        @pack = Pack.new
        @product = Product.new
        4.times {@ product.product_images.build}#添加了此
        respond_to代码做|格式|
          的format.html#new.html.erb
          format.xml {渲染:XML => @包 }
        结束
      结束

      高清create_starred_product
        产品= Product.new(PARAMS [:产品】)
        product.save
...

      结束
 

查看

 <%form_remote_for @product,:URL => {:控制器=> 产品,:操作=> create_starred_product}:HTML => {:多部分=>真}做| F | %>
      < D​​IV ID =内容>
....#其他范畴的产物。 ITS Save方法确定
        < D​​IV ID =照片选择器>
          <跨度类=fleft>Imágenes< / SPAN>< BR />
          &所述;%计数= 0%GT;
          <%f.fields_for:product_images办|制造商| %>

              <%如果builder.object.new_record? %>
                  <标签>
                    IMAGEN<%=#{数=计数+ 1}%>
                  < /标签>
                  <%= builder.file_field:照片,:类=> 文本区域 - %>< BR />
              <%结束%GT;
          <%结束%GT;
        < / DIV>
        &其中p为H.;&其中;%= f.submitCREAR%>&所述; / P>
      < / DIV>
  <%结束%GT;
 
关于springmvc 和ajax使用时ajax无效的问题

模式

 类产品<的ActiveRecord :: Base的
  的has_many:包:通过=> :pack_products
  的has_many:product_images,:依赖=> :破坏
  #回形针
  accepts_nested_attributes_for:product_images,:reject_if =>拉姆达{| T | T ['product_image']。空白? }
结束

类ProductImage<的ActiveRecord :: Base的
  belongs_to的:产品
  has_attached_file:照片,:URL => /:attachment/:class/:id/:style_:basename.:extension:风格=> {:中等=> 300×300>中,:拇指=&​​GT; 100×100>中,:小=> 30×30>中}
结束
 

解决方案

您无法上传使用AJAX的文件,就像你说的。有,但是,替代解决方案。

的常用技术来完成,这是一iframe的用法。看看此处,本教程是面向attachment_fu但它会用回形针正常工作。 Uploadify 是值得一试,也可以用曲别针上的导轨2.3 和的轨道3 。

I am reading this tutorial : http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/ because i need Save the product images in a independent model. However when Product.create execute don´t save the data for product images model.

NOTE: I use the new pack action because i need to use ajax for create a new product.

Please I need Help.

My Code

Controllers

    class Admin::PacksController < ApplicationController
      def new
        @pack = Pack.new
        @product = Product.new
        4.times {@product.product_images.build} # added this
        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @pack }
        end
      end

      def create_starred_product
        product = Product.new(params[:product])
        product.save
...

      end

View

<% form_remote_for @product, :url => {:controller => "products", :action => "create_starred_product"}, :html => {:multipart => true} do |f| %>
      <div id="content">
....#OTHER FIELDS OF PRODUCT. ITS SAVE METHOD IS OK
        <div id="images-selector">
          <span class="fleft">Imágenes</span><br/>
          <% count = 0 %>
          <% f.fields_for :product_images do |builder| %>

              <% if builder.object.new_record? %>
                  <label>
                    Imagen <%= "#{count = count + 1}" %>
                  </label>
                  <%= builder.file_field :photo, :class => "textarea" -%><br/>
              <% end %>
          <% end %>
        </div>
        <p><%= f.submit "Crear" %></p>
      </div>
  <% end %>

Models

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? }
end

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  

解决方案

You can't upload files using AJAX, just like you said. There are, however, alternate solutions.

The common technique to accomplish this is the usage of an iframe. Have a look here, this tutorial is oriented towards attachment_fu but it'll work with paperclip as well. Uploadify is worth checking out and can also be used with paperclip on rails 2.3 and rails 3.

 
精彩推荐
图片推荐