Ruby on rails carierwave在上载的文件路径上获取null

Ruby on rails carierwave在上载的文件路径上获取null,ruby-on-rails,ruby,ruby-on-rails-5,carrierwave,Ruby On Rails,Ruby,Ruby On Rails 5,Carrierwave,iam使用carierwave发布多部分/表单数据。 这是我的剧本 #imagepath model class Imagepath < ActiveRecord::Base belongs_to :imagepost attr_accessor :path mount_uploader :path, ImagepathUploader end #imagepost model class Imagepost < ActiveRe

iam使用carierwave发布多部分/表单数据。 这是我的剧本

#imagepath model 
   class Imagepath < ActiveRecord::Base
      belongs_to :imagepost
      attr_accessor :path
      mount_uploader :path, ImagepathUploader
    end

#imagepost model
class Imagepost < ActiveRecord::Base
  belongs_to :user
  has_many :imagepaths
  has_many :imagecomments
  has_many :imagelikes
  attr_accessor :imagepath_data
  # attr_accessor :path
end

    #imagepost controller post method
    # POST /imageposts
  def create
    @imagepost = Imagepost.new(imagepost_params)

    if @imagepost.save
      params[:imagepost][:imagepath_data].each do |file|
        @imagepost.imagepaths.create!(:path => file)
      end

      render json: @imagepost, status: :created, location: @imagepost
    else
      render json: @imagepost.errors, status: :unprocessable_entity
    end
  end


#imagepost_params for post_params
def imagepost_params
    params.require(:imagepost).permit(:title, :description, :user_id, :imagepath_data => [])
end
发布是工作,但在执行发布后,我从imagepath表中获取的路径行为空:

你可能失去了一个@:

编辑:另外,最好从Rails控制台bin/Rails c开始,检查DB:Imagepath.find17。它向您显示了实际保存的内容

我建议对详细输出使用和curl-v选项。 以下是来自真实项目的简化示例:

report.rb


谢谢,现在问题解决了。我还在imagepaths模型中添加了serialize:avatars和JSON,现在可以工作了。我正在起诉sqlite
curl 
-F "imagepost[imagepath_data][]=c4ewt.JPG" 
-F "imagepost[imagepath_data][]=border-image.png" 
-F "imagepost[title]=asasassasa" 
-F "imagepost[description]=uhuhuhuhuhuhuh" 
-F "imagepost[user_id]=5" localhost:3000/imageposts
-F "imagepost[imagepath_data][]=@c4ewt.JPG"
class Report < ApplicationRecord
  # ...
  accepts_nested_attributes_for :report_images,
    reject_if: proc { |attributes| attributes[ 'image' ].blank? }
end
class ReportImage < ApplicationRecord
  # ...
  mount_uploader :image, ReportImageUploader
end
class ReportsController < YourBaseController
  # ...
  def create
    # In real project service class is used
    report = Report.create!(create_params)
    # ...
  end

  private

  def create_params
    params
      .require(:report)
      .permit( 
        :my_report_attribute,
        report_images_attributes: [ :kind, :image ] )

  end
end
curl -XPOST -v http://lvh.me:3000/yourendpoint/reports \
  -F "report[my_report_attribute]=Hehe" \
  -F "report[report_images_attributes][0][kind]=haha" \
  -F "report[report_images_attributes][0][image]=@/Users/myuser/my0.jpg" \
  -F "report[report_images_attributes][1][kind]=hoho" \
  -F "report[report_images_attributes][1][image]=@/Users/myuser/my1.png"