Created
March 16, 2011 09:26
-
-
Save rvanlieshout/872229 to your computer and use it in GitHub Desktop.
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
def create | |
@user = User.find(params[:responsibility][:user_id]) | |
@company = Company.find(params[:company_id]) | |
# create an array of responsibilities that we want to save | |
responsibilities = params[:responsibility][:division_ids].collect do |division_id| | |
Responsibility.new(:division_id => division_id, :user_id => @user.id) | |
end | |
# get a list of invalid responsibilities | |
@invalid_responsibilities = responsibilities.collect do |responsibility| | |
responsibility unless responsibility.valid? | |
end.compact | |
if @invalid_responsibilities.length > 0 | |
# we have one or more invalid responsibilites | |
render :index and return | |
else | |
# all new responsibility are valid so delete the current ones | |
Responsibility.delete_all(:user_id => @user.id) | |
# and apply the new ones | |
responsibilites.each(&:save) | |
# continue... | |
flash[:notice] = t('successfully_changed_responsibilities') | |
redirect_to company_profile_responsibilities_path(@company, @user.profile) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment