Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Update object from params

I'd like to know if there is a way to bind params to an existing object in order to edit it. I'd like to do something like this, but to edit the object :

@object = Object.new(params[:object])

So something like :

@object = Object.find(params[:object_id])
@object.edit(params[:object])

params[:object] comes from a form_for

Any idea ?

like image 345
Aeradriel Avatar asked Dec 09 '22 06:12

Aeradriel


1 Answers

You can use

@object.assign_attributes(params[:object])

or

@object.attributes = params[:object]
like image 100
Ahmad Sherif Avatar answered Dec 22 '22 17:12

Ahmad Sherif