Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Created July 19, 2012 21:42
Show Gist options
  • Save guilleiguaran/3147039 to your computer and use it in GitHub Desktop.
Save guilleiguaran/3147039 to your computer and use it in GitHub Desktop.
Using attr_accessible: the right way
class UsersController < ApplicationController
include ActiveModel::MassAssignmentSecurity
attr_accessible :name, :age
attr_accessible :name, :age, :admin, :as => :admin
def create
@user = User.create!(user_params)
redirect_to @user
end
private
def user_params
role = current_user.admin? ? :admin : :default
sanitize_for_mass_assignment(params[:user], role)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment