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

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

问题描述

我的Rails 3应用程序中有一个处方模型。我试图找出允许记录重复的最佳方法,但允许用户在保存之前复查重复。



我已经读了一个数字关于SO的问题/答案(例如),其中说明了如何复制/克隆记录然后保存 - 但没有一个解释如何在保存之前显示表单。



阅读Rails API会出现克隆方法可用。



阅读其他问题和答案显示可以完成,但没有示例代码除外:

  new_record = old_record.dup 

控制器代码I a m目前正在使用如下(模型没有任何关系):

 #POST /处方
#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 |
如果@ prescription.save
format.html {redirect_to @prescription,通知:处方已成功创建}
format.json {render json:@prescription,status::created,位置:@prescription}
else
format.html {render action:new}
format.json {render json:@ prescription.errors,status::unprocessable_entity}
结束
结束
结束

我将链接到此克隆操作从

 <%= link_to创建另一个像这样?,clone_prescription_url(@prescription),:method => :put%> 

是否像这样为我的控制器添加一个操作一样简单?

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

道歉如果上述代码完全错误,我试图让我的头围绕它!我看到某人正在做我正在尝试的使用克隆实现 - 但不保存前的编辑。



复制的用户在保存后无权编辑记录。它完全用于初始数据输入。

解决方案

如果您希望克隆操作允许用户在它之前查看该副本保存(AKA创建),那么它几乎像新的动作,除了已经填写的领域。



所以你的克隆方法可能是你的新的方法:

  def new 
@prescription = Prescription.new()
end
def克隆
@prescription = Prescription.find(params [:id])#查找原始对象
@prescription = Prescription.new(@ prescription.attributes)#初始化重复(未保存)
呈现:new#渲染与new相同的视图,但@prescription属性已经填入
end

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


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.

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.

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天全站免登陆