2017-08-07 41 views
0

我有一個ImageAnalyses控制器,我想在之後執行一些代碼ImageAnalysis已被實例化,但在保存@image_analysis之前。儘管控制器正在成功創建ImageAnalysis實例,但它並未執行下面的中間代碼。部分代碼沒有在方法中執行

我的控制器:

#image_analyses_controller.rb 

def create 
@image_analysis = ImageAnalysis.new(image_analysis_params) 


# Start of not executed code 
@client = Aws::Rekognition::Client.new 
@image_analysis.gallery.attachments do |attachment| 
    resp = @client.detect_labels(
      image: 
       { s3_object: { 
       bucket: "my-bucket", 
       name: attachment.content.path, 
       }, 
      } 
     ) 

    high_labels = resp.labels.select { |label| label.confidence > 80 } 

    high_labels.each do |label| 
    ImageLabel.create(
     name: label.name, 
     image_url: attachment.content.path, 
     image_analysis_id: @image_analysis.id 
    ) 
    end 
end 
# End of not executed code 

respond_to do |format| 
    if @image_analysis.save 
    format.html { redirect_to @image_analysis, notice: 'Image analysis was successfully created.' } 
    format.json { render :show, status: :created, location: @image_analysis } 
    else 
    format.html { render :new } 
    format.json { render json: @image_analysis.errors, status: :unprocessable_entity } 
    end 
end 
end 

有趣的是沒有異常升高,服務器日誌只註冊一無所有的圖像分析對象指向了我一個錯誤的創建。

我試着將該塊代碼傳遞給模型中的一個方法,並從控制器調用它,結果相同。你能告訴爲什麼會發生這種情況嗎?

+0

你可以發佈'image_analysis_params'的內容嗎? – MatayoshiMariano

回答

0

我想你忘了.each你必須迭代@image_analysis.gallery.attachments

事情是這樣的:

@image_analysis.gallery.attachments.each do |attachment| 
    resp = @client.detect_labels(
      image: 
       { s3_object: { 
       bucket: "my-bucket", 
       name: attachment.content.path, 
       }, 
      } 
     ) 

    high_labels = resp.labels.select { |label| label.confidence > 80 } 

    high_labels.each do |label| 
    ImageLabel.create(
     name: label.name, 
     image_url: attachment.content.path, 
     image_analysis_id: @image_analysis.id 
    ) 
    end 
end 

但是,如果@image_analysis.gallery.attachments是空的,裏面.each的代碼,它不會被執行。

+0

哦,我的上帝,是的,問題是每個方法的缺失。感謝您指出。我需要休息!!! – Andrew