0

In my application I want people to be able to make a post, then be able to copy it, make changes to it, and then compare it to the previous post.

I looked at Difference between Dup and Clone Method

And it seems like these might work but I'm not certain which would be the best course of action. If I read it right I would probably want to go with clone. But I don't want the original to be overwritten so I should use dup instead?

Basically I have something like:

 def index
  @posts = Post.all
 end

 def new
  @post = Post.new
  @post.title = "original"
 end

 def copy
  @post = Post.new
  @post.dup
  @post.title = "something new"
  @post.save
end

Ideally the post with the title of "original" would exist and the method of copy would allow for everything from the original to be copied over, without overwriting the original, for review. Am I close?

user229044
  • 232,980
  • 40
  • 330
  • 338
Jake
  • 1,328
  • 1
  • 21
  • 51
  • I'd love to see how this is an exact duplicate of an existing question seeing as I've been looking for answer through Stack Overflow for some time about this sole issue. It's a very specific question. If there is an exact duplicate please let me know. – Jake Oct 11 '17 at 18:59
  • let's say you get the new version through an input field from a form in the view. You can retrieve it in the controller catching the param `@newpost=params[:postfield]` . then you can also load the original one `@originalpost=Post.find(params[:id])` , the id being provided in the address bar. you can then compare both in the same controller block... (I assume you have a Post model of course) – Maxence Oct 11 '17 at 19:49

0 Answers0