Skip to content

Instantly share code, notes, and snippets.

@robertomiranda
Forked from guilleiguaran/users_controller.rb
Created July 19, 2012 21:47
Show Gist options
  • Save robertomiranda/3147074 to your computer and use it in GitHub Desktop.
Save robertomiranda/3147074 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