Last active
January 4, 2016 04:29
-
-
Save mgidea/8568420 to your computer and use it in GitHub Desktop.
quick way to work through models with lots of attributes.
assume :user accepts_nested_attributes_for :profile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is in application_controller but could be in a module | |
NON_FORM_PARAMS = [:id, :created_at, :updated_at] | |
def permitted_params(model) | |
model.column_names - NON_FORM_PARAMS | |
end | |
def nested_destroy(model) | |
permitted_params(model) + [:destroy, :id] | |
end | |
class UsersController < ApplicationController | |
.... | |
def user_params | |
params.require(:user).permit(*(permitted_params(User) + [:profile_attributes => nested_destroy(Profile)] - [:roles])) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment