Rails clone copy or duplicate

后端 未结 4 1542
孤独总比滥情好
孤独总比滥情好 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
    
    0 讨论(0)
  • 2020-12-31 10:57

    Also worth mentioning is the dup method on a model. It makes a copy with all attributes and outgoing relations but sets id to nil. Like this (borrowing code from Naren Sisodiya):

    def create_from_existing
      @existing_post = Post.find(params[:id])
      #create new object with attributes of existing record 
      @post = @existing_post.dup
      render "your_post_form"
    end
    
    0 讨论(0)
  • 2020-12-31 11:00

    I found these answers a little hard to follow. One answer shows this:

    @post = Post.new(@existing_post.attributes)
    

    which will not work as it will also pass the id, and timestamp values. I used .dup to fix that and I show that in my answer.

    Here's how I achieved creating a new item from an existing item.

    The model is for a Product, the controller Products_Controller.rb. We're going to add a new action to the controller called COPY and we're going to link to it from the SHOW view on an existing Product and render a filled out NEW view ready to be edited and saved.

    First we create a route for the copy action in routes.rb

    resources :Products do
        member do
          get 'copy'
        end
      end
    

    Then a copy action in Products_controller.rb

     def copy
            @source = Product.find(params[:id])
            @product = @source.dup
            render 'new'
          end
    

    Now we need to add a Link to the SHOW view to call our copy action.

    <%= link_to "copy", copy_product_path(params[:id]) %>
    

    This worked for me. I hope it works for you and that the answer is simple enough to follow.

    0 讨论(0)
  • 2020-12-31 11:02

    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
    
    0 讨论(0)
提交回复
热议问题