【问题标题】:Rails how to build on iterationRails 如何在迭代上构建
【发布时间】:2023-03-22 13:04:01
【问题描述】:

尝试使用 dropzone 保存一组图像而无需迭代。我切换到迭代图像并尝试保存循环。但是铁轨会吐

ActiveRecord::StatementInvalid 在 /products ============================================> SQLite3::ConstraintException: NOT NULL 约束失败: images.product_id: INSERT INTO "images" ("created_at", "ifoto", “product_data_id”、“product_id”、“updated_at”)值(?,?,?,?,?) 应用程序/控制器/products_controller.rb,第 214 行 ------------------------------------------------ @987654321 @App 回溯 ------------- - 应用程序/控制器/products_controller.rb:214:in block (3 levels) in create' - app/controllers/products_controller.rb:211:inblock (2 级别)在创建' - app/controllers/products_controller.rb:137:in `create' 完整的回溯 --

这里是放置区代码

 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

有人对这类问题有提示吗?

【问题讨论】:

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


    【解决方案1】:

    您的问题缺少有关模型和关系的信息。 由于知识太少,我只是猜测。 试试这个:

    @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 }
    

    【讨论】:

      猜你喜欢
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      相关资源
      最近更新 更多