Skip to content

Instantly share code, notes, and snippets.

@tubbo
Forked from karapetyan/example_from_book
Created July 16, 2012 17:24
Show Gist options
  • Save tubbo/3123884 to your computer and use it in GitHub Desktop.
Save tubbo/3123884 to your computer and use it in GitHub Desktop.
Mass-assigment attr_accessable
class UsersController < ApplicationController
respond_with :html
def create
@user = User.new(params[:user])
if @user.save
respond_with [:admin, @user], notice: "User succesfully created!"
else
redirect_to 'admin/users/new', alert: "User was not created: #{@user.errors.full_messages.join(',')}"
end
end
end
class User < ActiveRecord::Base
attr_accessible :admin
def admin=(raw_value)
is_admin = (raw_value == "1")
self.write_attribute(:admin, is_admin)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment