Rails clone copy or duplicate

后端 未结 4 1566
孤独总比滥情好
孤独总比滥情好 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: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
    

提交回复
热议问题