Rails克隆副本或副本 [英] Rails clone copy or duplicate

查看:96
本文介绍了Rails克隆副本或副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的表单,保存后,我希望能够单击显示页面上的链接以复制或克隆该表单并打开一个新表单.从那里,我应该能够进行编辑(如新ID)并另存为新记录.我已经看到了一些类似 deep_cloneable gem 的示例,但是我不知道如何实现它.我认为这应该很简单,但是我只是不明白在控制器和显示视图中放置内容的位置.

I have a nested form and once I save, I want to be able to click a link on the show page to copy or clone that form and open a new one. From there I should be able to make edits (like a new id) and save as a new record. I have seen some examples like this deep_cloneable gem, but I have no idea how to implement it. I think this should be simple, but I just don't understand where to put things in the controller and in the show view.

推荐答案

如果要复制activeRecord对象,可以使用其属性来创建新对象,例如

If you want to copy an activeRecord object you can use its attributes to create new one like

您可以在控制器中执行一个操作,该操作可以在链接上调用

you can have an action in your controller which can be called on link,

def  create_from_existing
 @existing_post = Post.find(params[:id])
 #create new object with attributes of existing record 
 @post = Post.new(@existing_post.attributes) 
 render "your_post_form"
end

这篇关于Rails克隆副本或副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆