Я читаю этот учебник: http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/ , потому что мне нужно сохранить изображения продукта в независимой модели. Однако при выполнении Product.create не сохранять данные для модели изображений продукта.Проблема скрепки с ajax
ПРИМЕЧАНИЕ: Я использую новое действие пакета, потому что мне нужно использовать 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. Теперь я прочитал, что скрепка и ajax не работают вместе (модель безопасности javascript), поэтому мне нужно альтернативное решение. Спасибо – maxiperez