Rails如何构建迭代

时间:2017-02-14 20:59:43

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

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

  

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行   ------------------------------------------------ {{ 1}} App backtrace ------------- -   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块中(2   创造' - app / controllers / products_controller.rb:137:in   `创建'完全回溯 -

这是dropzone代码

block (3 levels) in
  create' - app/controllers/products_controller.rb:211:in

有人暗示这种问题吗?

1 个答案:

答案 0 :(得分: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 }