复制一个记录的Rails 3 [英] Duplicating a record in Rails 3

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

问题描述

我在我的Rails一个prescription模型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答案(如之一),它解释了如何复制/克隆的记录,然后将其保存 - 但没有解释如何显示窗体之前保存

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是出现在克隆的方法是可行的。

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

读<一href="http://stackoverflow.com/questions/60033/what-is-the-easiest-way-to-duplicate-an-activerecord-record?lq=1">other问题和答案显示是可以做到的,但没有例如code距:

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

new_record = old_record.dup

控制器code我目前正在与如下(模型没有任何关系):

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

如果上面的code是完全错误的,我试图让我的头周围的歉意!我已经看到了<一个href="http://stackoverflow.com/questions/8555488/rails-duplicate-record-not-rendering-new?rq=1">someone做的正是我试图实现与克隆 - 但与编辑之前保存

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.

就是复制将无法修改一次保存记录权限的用户。这纯粹是为了intial数据输入。

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

推荐答案

如果你想克隆的行动,允许用户查看重复之前,它被保存(AKA创建),那么它几乎是像新行动除了与填充已领域。

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