I ignored this problem for a while, but I can no longer. My product_images nested model objects are not displayed when I render edit view of the product. Because of this, my application throws an error stating that the image is zero when I try to display the show view.
Why do my images of embedded products come about? I use paperclip and s3.
I need a form of nested images for editing, everything works when a new product is created, the only problem is editing. The nil exception during the show action is simply due to a malfunction of the edit action
Below is my code:
model ##
class ProductImage < ActiveRecord::Base attr_accessible :product_id, :photo, :image_width, :image_height belongs_to :product has_attached_file :photo, :styles => { :small => "150x150>"}, :dependent => :destroy validates_attachment_presence :photo validates_attachment_size :photo, :less_than => 5.megabytes validates_attachment_content_type :photo, :content_type => ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'] after_post_process :save_image_dimensions def save_image_dimensions geo = Paperclip::Geometry.from_file(photo.queued_for_write[:original]) self.image_width = geo.width.to_i self.image_height = geo.height.to_i end def sized_different? if image_width < image_height true else false end end end class Product < ActiveRecord::Base attr_accessible :taxable, :shippable, :page_name, :tact_code, :manu_code, :body_code, :name, :alert, :price, :description, :colors_attributes, :sizes_attributes, :extras_attributes, :restraints_attributes, :product_images_attributes validates_presence_of :page_name validates_presence_of :price validates_presence_of :description validates_presence_of :name has_many :product_images, :dependent => :destroy has_many :restraints, :dependent => :destroy has_many :colors, :dependent => :destroy has_many :sizes, :dependent => :destroy has_many :extras, :dependent => :destroy accepts_nested_attributes_for :colors, :sizes, :extras, :restraints, :product_images end
controller
def index @products = Product.search(params) unless current_cart.nil? @cart = current_cart else @cart = nil end end def new @product = Product.new @product.product_images.build end def create @product = Product.new(params[:product]) if @product.save render :index else render :new end end def edit @product = Product.find(params[:id]) @cart = current_cart end def show @product = Product.find(params[:id]) @cart = current_cart end def update @product = Product.find(params[:id]) if @product.update_attributes!(params[:product]) render :index else render :edit end end end
_form view
<%= form_for @adminproduct, validate: true, html:{ multipart: true} do |f| %> <div class="span3 field"> <ul class="unstyled"> <li><%= f.text_field :name, :placeholder => "Product name" %></li> <li><%= f.text_field :manu_code, :placeholder => "Manufacturer code" %></li> <li><%= f.text_field :alert, :placeholder => "Alert, example: Save 10%" %></li> <li><%= f.text_field :price, :placeholder => "Base price" %></li> <li><%= f.text_area :description, :placeholder => "Product description", :size => "30x10" %></li> </ul> </div> <div class="span6 offset1"> <ul class="unstyled"> <li><%= f.check_box :taxable %>Item can be taxed</li> <li><%= f.check_box :shippable%>Item can be standard shipped</li> <li><br /><b>Choose product images:</b></li> (the first image will be the product main image)<br/> <%= f.fields_for :product_images do |p|%><%= render 'product_image_fields', f: p %><% end %> <li><%= link_to_add_fields "Add image", f, :product_images %></li>
Any understanding is understood.
Edit
partial product images
<fieldset> <%= f.file_field :photo %> <%= f.hidden_field :_destroy %> <%= link_to "remove", '#', class: "remove_fields" %> </fieldset>