I have a button which label is "save & copy" doing save the current object (@student) and redirect it back to new action and these new contains all the previous object attributes. I have stucked how to populate previous values.
Is ROR provide any such method to make clone of obje开发者_开发知识库ct.
Ruby on Rails provides the clone
method, which creates a shallow clone of the object. In your case, you could use it like that:
@previous_student = @student.clone
The documentation of the clone
method states:
Returns a clone of the record that hasn’t been assigned an id yet and is treated as a new record. Note that this is a “shallow” clone: it copies the object’s attributes only, not its associations. The extent of a “deep” clone is application-specific and is therefore left to the application to implement according to its need.
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001848
As stated in the documentation, you should maybe override the clone
method, to implement your own cloning, if you use associations in the Student
model.
Cheers
精彩评论