通过 Paperclip 上传图像而不保存对象

Upload image through Paperclip without saving object

我在 Rails 工作,我有两个模型,一个是预发布模型,一个是倡议模型。基本上我希望用户能够使用预启动的属性创建一个计划。基本上我想要发生的是,当用户访问他们的预发布并准备将其变成一项计划时,它会将他们带到一个已经填充了他们的预发布信息的表单,他们可以添加额外的信息。到目前为止,我已经为每个属性都做到了这一点,除了附加的图像,称为:cover_image。

我认为问题是我在我的控制器的新动作上将倡议的 cover_image 设置为预启动的 cover_image,但因为这是新动作而不是创建,我尚未保存主动权。我认为这意味着 cover_image 还没有被重新上传,所以 @iniative.cover_image.url 没有指向任何东西。它似乎也没有用任何东西预先填充我表单的文件字段。

我不完全确定这一切的可行性,但这是客户要求的,所以我正在努力让它对他们有用。

这是我的控制器:

def new
  @initiative = Initiative.new
  populate_defaults(@initiative)
  @initiative.build_location
  3.times{ @initiative.rewards.build }
  @initiative.user = current_user
  if !params[:prelaunch_id].nil? && !params[:prelaunch_id].empty?
    # if user is transferring a prelaunch, assign its attributes to the intiative
    @prelaunch = Prelaunch.find(params[:prelaunch_id])
    @initiative.assign_attributes(title: @prelaunch.title,
                                teaser: @prelaunch.teaser,
                                category: @prelaunch.category,
                                funding_goal: @prelaunch.funding_goal,
                                term: @prelaunch.campaign.term,
                                story: @prelaunch.story,
                                location: @prelaunch.campaign.location,
                                video_url: @prelaunch.video_url,
                                EIN: @prelaunch.campaign.EIN,
                                nonprofit: @prelaunch.nonprofit,
                                organization_name: @prelaunch.campaign.organization.name)
  end
end

编辑:

感谢 peterept 在下面的回答,我已经设法将预启动 cover_image 放入表单中并放入倡议控制器的创建操作中。现在的问题是,在创建操作中,一切似乎都完美无缺:主动获取预启动的封面图像,保存无误,然后重定向到显示操作。

不幸的是,当它到达控制器的显示操作时,@initiative.cover_image 再次设置为默认值。我不知道在成功的创建操作和显示操作之间可能发生了什么。

以下是倡议控制器的创建和显示操作:

  def create
    if !params[:initiative][:prelaunch_id].nil? && !params[:initiative][:prelaunch_id].empty?
      @prelaunch = Prelaunch.find(params[:initiative][:prelaunch_id]) # find the prelaunch if it exists
    end
    @initiative = Initiative.new(initiatives_params)
    @initiative.user = current_user
    begin
      @payment_processor.create_account(@initiative)
      if @initiative.save
        # @prelaunch.destroy # destroy the prelaunch now that the user has created an initiative
        flash[:alert] = "Your initiative will not be submitted until you review the initiative and then press 'Go Live' on the initiative page"
        redirect_to initiative_path(@initiative)
      else
        flash[:alert] = "Initiative could not be saved: " + @initiative.errors.messages.to_s
        render :new
      end
    rescue Exception => e
      logger.error e.message
      flash[:error] = "Unable to process request - #{e.message}"
      render :new
    end
  end

  def show
    @initiative = Initiative.find(params[:id])
    @other_initiatives = Initiative.approved.limit(3)
  end

这是来自同一控制器的 initiatives_params 方法:

def initiatives_params
  initiative_params = params.require(:initiative).permit(
    :terms_accepted,
    :title,
    :teaser,
    :term,
    :category,
    :funding_goal,
    :funding_type,
    :video_url,
    :story,
    :cover_image,
    :nonprofit,
    :EIN,
    :role,
    :send_receipt,
    :organization_name,
    :crop_x, :crop_y, :crop_h, :crop_w,
    location_attributes: [:address],
    rewards_attributes: [:id, :name, :description, :donation, :arrival_time, :availability, :_destroy, :estimated_value])
  if @prelaunch.media.cover_image
    initiative_params[:cover_image] = @prelaunch.media.cover_image
  end
  initiative_params
end

您可以传递图片URL并显示在页面上。

然后用户可以通过上传新图像(按照正常情况)来覆盖它。

在您创建操作时,如果他们没有提供新图片,则将其设置为相关预启动中的图片 - 您需要复制原始图片,这样上传时就不会被替换一个新的。 (如果您不知道哪个是预发布,您可以将 ID 传递给页面)。

我可以通过仅保存 Paperclip 对象来使其工作。这是我的模型:

class Grade < ActiveRecord::Base
  has_attached_file :certificate
end

如果我 运行 以下内容:

@grade.certificate = new_file
@grade.certificate.save

它 saves/overwrite 文件,但不更新 Grade 对象。

版本:ruby-2.3.8Rails 4.2.11.3paperclip (4.3.6)