Rails 克隆副本或复制 [英] Rails clone copy or duplicate
问题描述
我有一个嵌套表单,保存后,我希望能够单击显示页面上的链接来复制或克隆该表单并打开一个新表单.从那里我应该能够进行编辑(如新 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屋!