Skip to content

Instantly share code, notes, and snippets.

@mgidea
Last active January 4, 2016 04:29
Show Gist options
  • Save mgidea/8568420 to your computer and use it in GitHub Desktop.
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 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