Duplicating a record in Rails 3

后端 未结 3 1189
耶瑟儿~
耶瑟儿~ 2020-12-19 02:54

I have a prescription model in my Rails 3 application. I am trying to work out the best method of allowing records to be duplicated, but allowing the user to \"review\" the

3条回答
  •  轮回少年
    2020-12-19 03:45

    To do this, you're going to have to create a new instance of your Prescription class. "dup" works, but you're assuming it overwrites the existing record. Only methods that end with a bang(!) tend to do that.

    Your code should be:

    def clone
     @prescription = Prescription.find(params[:id])
     @new_prescription = @prescription.dup
     @new_prescription.save
    end
    

    or

    def clone
     @prescription = Prescription.find(params[:id]).dup
     @prescription.save
    end
    

    This isn't testing for times when the :id isn't found.

提交回复
热议问题