2017-02-14 114 views
1

尝试使用dropzone保存一组图像而不进行迭代。我切换到迭代图像并尝试保存在循环中。但铁轨随地吐痰Rails如何在迭代上构建

ActiveRecord :: StatementInvalid at/products ================================= ==========> SQLite3 :: ConstraintException:NOT NULL约束失败: images.product_id:INSERT INTO“images”(“created_at”,“ifoto”, “product_data_id”,“product_id”,“ updated_at“)VALUES(?,?,?,?,?) app/controllers/products_controller.rb,214行 ----------------------- ------------------------- ruby 209 210 if params[:images] && params[:images][:ifoto] 211 params[:images][:ifoto].values.each do |ifoto| 212 213 @image.ifoto = ifoto > 214 @image.save 215 216 217 end 218 render :nothing => true 219 App backtrace ------------- - app/controllers/products_controller.rb :214:在block (3 levels) in create' - app/controllers/products_controller.rb:211:in块(2 级别)中创建' - app/controllers/products_control ler.rb:137:在 '打造”全回溯 -

这里是悬浮窗代码

format.json do 
    @product = current_vitrine.products.build(params[:product]) 
     @image = @product.images.build(params[:images]) 



if params[:images] && params[:images][:ifoto] 
     params[:images][:ifoto].values.each do |ifoto| 

@image.ifoto = ifoto 
    @image.save 


        end 
    render :nothing => true 

    end 
    end 

有人有一个提示这类问题?

回答

0

您的问题缺乏关于模型和关系的信息。有了这么少的知识,我只能猜测。 试试这个:

@product = current_vitrine.products.build(params[:product]) 

if @product.save 
    if params[:images] && params[:images][:ifoto] 
    params[:images][:ifoto].values.each do |ifoto| 
     @product.images.create(ifoto: ifoto) 
    end 
    end 
end 

format.json { render :nothing => true } 
+0

非常感谢你! – japalow