Rails克隆副本或副本

编程入门 行业动态 更新时间:2024-10-27 06:18:39
本文介绍了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克隆副本或副本

本文发布于:2023-11-09 16:05:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572817.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:副本   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!