Rails 4.0 with Devise. Nested attributes Unpermited parameters

前端 未结 3 1393
日久生厌
日久生厌 2020-11-29 03:07

I am working on a web-app using Devise and Rails 4. I have a User model which I have extended with 2 extra form fields such that when a user signs up he can

3条回答
  •  爱一瞬间的悲伤
    2020-11-29 03:54

    Using rails 5.1 and devise 4.4.1 following is the shortest and works pretty good:

    app/models/user.rb

    after_initialize do
      build_profile if new_record? && profile.blank?
    end
    

    app/controllers/application_controller.rb

    before_action :configure_permitted_parameters, if: :devise_controller?
    
    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [{ profile_attributes: :name }])
    end
    

    The key here is that you can do following without making separate controller:

    • permit nested attributes
    • build relation for form builder

提交回复
热议问题