ruby-on-rails – 在编辑操作中未显示的嵌套图像模型对象
发布时间:2020-12-17 02:04:57 所属栏目:百科 来源:网络整理
导读:我已经暂时忽略了这个问题,但我不能再这样了.我渲染产品的编辑视图时,我的嵌套图像模型对象product_images没有显示.由于这个原因,我的应用程序调用一个错误,表示当我尝试渲染显示视图时图像为零. 为什么我的嵌套产品图片没有出现?我正在使用paperclip和s3.
我已经暂时忽略了这个问题,但我不能再这样了.我渲染产品的编辑视图时,我的嵌套图像模型对象product_images没有显示.由于这个原因,我的应用程序调用一个错误,表示当我尝试渲染显示视图时图像为零.
为什么我的嵌套产品图片没有出现?我正在使用paperclip和s3. 我需要嵌套图像表格可编辑,一切都在创建新产品时有效,编辑是唯一的问题. show动作期间的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> 解决方法
在products_controller上,修改编辑操作以包含product_images,以便
accepts_nested_attributes_for知道要更新的图像.
def edit @product = Product.includes(:product_images).find(params[:id]) # ... end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |