Nested image model objects that are not displayed in the editing action

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> 
+4
source share
3 answers

In product_controller, change the editing action to include product_images so that accepts_nested_attributes_for knows which images to update.

 def edit @product = Product.includes(:product_images).find(params[:id]) # ... end 
+1
source

I believe you need to call your partial, like this:

 <%= render partial: 'product_image_fields', locals: {f: p} %> 

If this does not work, insert your product_image_fields file and an error message from the console in your original question.

-

Ok this is your partial

 <fieldset> <%= f.file_field :photo %> <%= f.hidden_field :_destroy %> <%= link_to "remove", '#', class: "remove_fields" %> </fieldset> 

Your problem has empty input to load product_image. Well, you use file_field, which produce input type=file , and HTML will not allow you to put a value in such fields. In other words: you cannot load a value from the database into file_field .

This is my advice:

Change your partial to get what comes from the database. It is just to check and confirm that the data is coming to the right place.

 <fieldset> <%= f.object.send :photo %> <%= f.file_field :photo %> <%= f.hidden_field :_destroy %> <%= link_to "remove", '#', class: "remove_fields" %> </fieldset> 

You should get the path of your image on the screen. Also look at the HTML source, Rails should add hidden fields with image record IDs.

If everything is ok, you need to display your image with something else than file_field , try image_tag or anything else you want to use

0
source

Have you tried to use the wonderful accepts_nested_attributes_for provided by Rails to work with Nested Attributes ?

Read more http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

EDIT I thought it was best to provide you with an example.

 class Product < ActiveRecord::Base attr_accessible ..., :product_images_attributes # <======= Pay attention here. Mass Assignment attributes required accepts_nested_attributes_for :product_images .... end 
0
source

Source: https://habr.com/ru/post/1491557/


All Articles