ruby-on-rails – 使用Paperclip附件复制记录

我正在创建一个复制项目的操作,然后允许用户编辑它并将其保存回数据库.

我在我的控制器中编写了以下方法,它主要与Paperclip附件不同,后者由于某种原因不会移动.

def duplicate
  existing_event = Event.find(params[:id])
  @event = Event.new(existing_event.attributes)

  render action: 'new'
end

我已经看到了这个人正在使用.dup的this question但我似乎无法在用户在保存之前编辑新项目的情况下工作.

我也试过使用像@ event.image = existing_event.image这样的东西,但也没有任何效果.

这是我的create方法的样子:

def create
  @event = Event.create(event_params)

  if @event.save
    redirect_to events_path,notice: "Event was successfully created."
  else
    render action: 'new'
  end
end

如果它有任何区别我也使用S3进行图像上传,如果那里有多个图像副本,那对我来说并不重要.

有人可以帮忙吗?谢谢!

解决方法

通过附件params就是这样:通过params.
您需要传递文件本身.
下面你会想到如何做到这一点,而不是测试它,但你可以玩它并让它工作,它应该不那么难.

关于新行动:

existing_event = Event.find(params[:id])
@event = Event.new(existing_event.attributes)

@event.image = File.open(existing_event.image.path,'rb')

render :action => 'new'

也:检查你的创建操作,你有一个轻微的错误,调用创建和保存相同的记录 – 这是多余的.您应该调用@ event = Event.new(event_params),然后调用@ event.save.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...