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

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -