이 자습서는 http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/ 이므로 독립적 인 모델로 저장해야합니다. 그러나 Product.create가 실행될 때 제품 이미지 모델의 데이터를 저장하지 마십시오.아약스가있는 문제 클립
참고 : 새 제품을 만들 때 ajax를 사용해야하므로 새 팩 작업을 사용합니다.
제발 도와주세요.
내 코드
컨트롤러
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
보기
<% 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 %>
모델
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
실례합니다. 제목에 오류가 있습니다. 실감하지 않았습니다. 문제는 클립과 Ajax에 관한 것입니다. 이제 나는 클립 클립을 읽었고 아약스는 (자바 스크립트 보안 모델) 함께 작동하지 않으므로 대안 솔루션이 필요하다. 감사합니다 – maxiperez