diff --git a/app/controllers/aspect_memberships_controller.rb b/app/controllers/aspect_memberships_controller.rb index 69b6c07d9..6bb565d1d 100644 --- a/app/controllers/aspect_memberships_controller.rb +++ b/app/controllers/aspect_memberships_controller.rb @@ -17,7 +17,7 @@ class AspectMembershipsController < ApplicationController @contact = current_user.contact_for(Person.where(:id => @person_id).first) membership = @contact ? @contact.aspect_memberships.where(:aspect_id => @aspect_id).first : nil - if membership && membership.destroy + if membership && membership.destroy @aspect = membership.aspect flash.now[:notice] = I18n.t 'aspect_memberships.destroy.success' @@ -76,4 +76,8 @@ class AspectMembershipsController < ApplicationController render :text => response_hash.to_json end + rescue_from ActiveRecord::RecordNotUnique do + render :text => "Duplicate record rejected.", :status => 400 + end + end diff --git a/spec/controllers/aspect_memberships_controller_spec.rb b/spec/controllers/aspect_memberships_controller_spec.rb index 7d4cb8c5c..b190f728d 100644 --- a/spec/controllers/aspect_memberships_controller_spec.rb +++ b/spec/controllers/aspect_memberships_controller_spec.rb @@ -13,7 +13,7 @@ describe AspectMembershipsController do @contact = alice.contact_for(bob.person) alice.getting_started = false alice.save - sign_in :user, alice + sign_in :user, alice @controller.stub(:current_user).and_return(alice) request.env["HTTP_REFERER"] = 'http://' + request.host end @@ -62,6 +62,13 @@ describe AspectMembershipsController do flash[:error].should_not be_empty end + it 'does not 500 on a duplicate key error' do + params = {:format => 'js', :person_id => @person.id, :aspect_id => @aspect0.id} + post :create, params + post :create, params + response.status.should == 400 + end + context 'json' do it 'returns a list of aspect ids for the person' do post :create,