首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过剪纸上传图像而不保存对象

通过剪纸上传图像而不保存对象
EN

Stack Overflow用户
提问于 2015-02-11 01:52:45
回答 2查看 539关注 0票数 0

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

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

我不完全确定这一切是否可行,但这是客户的要求,所以我试图让它为他们工作。

这是我的控制器:

代码语言:javascript
复制
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放到了表单中,并进入了主动控制器的创建操作。现在的问题是,在create操作中,一切看起来都是完美的:主动权获得了预启动的封面图像,它没有错误地保存,并且重定向到显示动作。

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

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

代码语言:javascript
复制
  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方法:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-11 02:16:02

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

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

在您的创建动作,如果他们没有提供一个新的图像,然后设置为在assocoiated的预启动-你会想要复制原来的,这样它就不会被取代,如果他们上传一个新的。(如果您不知道哪个是预启动,则可以将ID向下传递到页面)。

票数 0
EN

Stack Overflow用户

发布于 2020-07-23 17:44:24

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

代码语言:javascript
复制
class Grade < ActiveRecord::Base
  has_attached_file :certificate
end

如果我运行以下命令:

代码语言:javascript
复制
@grade.certificate = new_file
@grade.certificate.save

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28445274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档