【问题标题】:Upload image through Paperclip without saving object通过回形针上传图像而不保存对象
【发布时间】:2015-02-11 01:52:45
【问题描述】:

我在 Rails 中工作,我有两个模型,一个是预发布模型,一个是倡议模型。基本上,我希望用户能够使用预发布的属性来创建倡议。基本上我想要发生的是当用户访问他们的预发布并准备将其转变为一项计划时,它会将他们带到一个已经填充了他们的预发布信息的表单,他们可以添加其他信息。到目前为止,我已经设法为每个属性执行此操作,除了附加的图像,称为: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 在下面的回答,我已经设法将预发布的封面图像放入表单并放入倡议控制器的创建操作中。现在的问题是,一切似乎都在 create 动作中完美运行:主动获取预发布的封面图像,它没有错误地保存,并重定向到 show 动作。

不幸的是,当它到达控制器的显示操作时,@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

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    您可以传递图像 URL 并将其显示在页面上。

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

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

    【讨论】:

      【解决方案2】:

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

      class Grade < ActiveRecord::Base
        has_attached_file :certificate
      end
      

      如果我运行以下命令:

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

      它保存/覆盖文件,但不更新 Grade 对象。

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

      【讨论】:

        猜你喜欢
        • 2016-03-31
        • 2011-05-02
        • 1970-01-01
        • 1970-01-01
        • 2010-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多