Rails clone copy or duplicate

后端 未结 4 1543
孤独总比滥情好
孤独总比滥情好 2020-12-31 10:19

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 e

4条回答
  •  一生所求
    2020-12-31 10:50

    class Foo < ActiveRecord::Base
      def self.clone_from(parent)
        parent = find(parent) unless parent.kind_of? Foo
        foo = self.new
        foo.attributes = parent.attributes
        # if you want to also clone a habtm:
        foo.some_association_ids = parent.some_association_ids
        # etc.
        foo
      end
    end
    
    class FoosController < ApplicationController
      def clone
        foo = Foo.clone_from(params[:id])
        respond_with(foo)
      end
    end
    

提交回复
热议问题