在 Rails 3 中复制记录 [英] Duplicating a record in Rails 3

查看:16
本文介绍了在 Rails 3 中复制记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Rails 3 应用程序中有一个处方模型.我正在尝试找出允许复制记录的最佳方法,但允许用户在保存副本之前查看"副本.

I have a prescription model in my Rails 3 application. I am trying to work out the best method of allowing records to be duplicated, but allowing the user to "review" the duplicate before it's saved.

我已经阅读了许多关于 SO 的问题/答案(例如 this一个)解释了如何复制/克隆记录然后保存它 - 但没有解释如何在保存前显示表单.

I have read a number of questions/answers on SO (such as this one) which explain how to duplicate/clone the record and then save it - but none which explain how to show the form before save.

阅读 Rails API 会出现 clone 方法可用.

Reading the Rails API is appears the clone method is available.

阅读其他问题 和答案表明这是可以完成的,但除了以下内容之外没有示例代码:

Reading other questions and answers shows that is can be done but there is no example code apart from:

new_record = old_record.dup

我目前使用的控制器代码如下(模型没有任何关系):

The controller code I am currently working with is as follows (the model doesn't have any relationships):

  # POST /prescriptions
  # POST /prescriptions.json
  def create
    @prescription = Prescription.new(params[:prescription])
    @prescription.localip = request.env['REMOTE_ADDR']
    @prescription.employee = @prescription.employee.upcase

    respond_to do |format|
      if @prescription.save
        format.html { redirect_to @prescription, notice: 'Prescription was successfully created.' }
        format.json { render json: @prescription, status: :created, location: @prescription }
      else
        format.html { render action: "new" }
        format.json { render json: @prescription.errors, status: :unprocessable_entity }
      end
    end
  end

我将从视图中链接到此克隆操作:

I am going to be linking to this clone action from the view with:

<%= link_to "Create another like this?", clone_prescription_url(@prescription), :method => :put %>

像这样向我的控制器添加一个动作那么简单吗?

Is it as simple as adding an action to my controller like this?

def clone
 @prescription = Prescription.find(params[:id])
 @prescription.dup
 @prescription.save
end

如果上面的代码完全错误,我很抱歉,我正在努力解决它!我见过有人做我正在尝试的事情用克隆来实现 - 但不是用保存前的编辑.

Apologies if the above code is completely wrong, I'm trying to get my head around it! I've seen someone do exactly what I'm trying to achieve with the cloning - but not with the editing before save.

复制的用户一旦保存就无权编辑记录.纯粹用于初始数据输入.

The user that's duplicating won't have permission to edit a record once saved. It's purely for the intial data entry.

推荐答案

如果您希望克隆操作允许用户在保存(也称为创建)之前查看副本,那么它几乎就像新建"操作, 除了已经填写的字段.

If you want the clone action to allow the user to review the duplicate before it is saved (AKA created), then it is almost like the "new" action, except with filled in fields already.

因此您的克隆方法可能是对新方法的修改:

So your clone method could be a modification of your new method:

def new
  @prescription = Prescription.new()
end
def clone
  @prescription = Prescription.find(params[:id]) # find original object
  @prescription = Prescription.new(@prescription.attributes) # initialize duplicate (not saved)
  render :new # render same view as "new", but with @prescription attributes already filled in
end

在视图中,他们可以创建对象.

In the view, they can then create the object.

这篇关于在 Rails 3 中复制记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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