我已经忽略了这个问题有一段时间了,但我再也不能了。当我查看产品视图时,我的嵌套图像模型对象product_images
没有显示。因此,当我尝试渲染视图时,我的应用程序调用了一个错误,指出图像为零。render
edit
show
为什么我的嵌套产品图片没有出现?我正在使用回形针和 s3。
我需要可编辑的嵌套图像表单,创建新产品时一切正常,编辑是唯一的问题。显示动作期间的 nil 异常仅与编辑动作故障有关
下面是我的代码:
楷模 ##
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
控制器
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 视图
<%= 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's 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>
任何见解都值得赞赏。
编辑
产品图片部分
<fieldset>
<%= f.file_field :photo %>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>