ruby - Override one attribute in Rails strong params -
is there way can override 1 attribute yet still use strong parameters in rails? such in following example, if wanted override person's name every time, use person_params
method. there has better way manually setting @person.name
, right?
class peoplecontroller < actioncontroller::base def create @person = person.new(person_params) @person.name = "abc" @person.save end private def person_params params.require(:person).permit(:name, :age) end end
if want override name other string, or format before saving, can do:
@person = person.new(person_params.merge!(name: 'abc'))
if want create , merge in 1 line, this:
def create @person = person.create(person_params.merge!(name: 'abc')) end
Comments
Post a Comment