diff --git a/app.rb b/app.rb index 19714a01..ef1b72e1 100644 --- a/app.rb +++ b/app.rb @@ -233,7 +233,13 @@ end post '/tags/add' do require_login current_site.new_tags_string = params[:tags] - current_site.save + + if current_site.valid? + current_site.save + else + flash[:errors] = current_site.errors[:tags].first + end + redirect request.referer end diff --git a/models/site.rb b/models/site.rb index ec406de3..35f309f9 100644 --- a/models/site.rb +++ b/models/site.rb @@ -386,7 +386,6 @@ class Site < Sequel::Model if @new_tags_string new_tags = @new_tags_string.split ',' - new_tags.uniq! new_tags.compact! @new_filtered_tags = [] @@ -416,7 +415,10 @@ class Site < Sequel::Model break end + next if tags.collect {|t| t.name}.include? tag + @new_filtered_tags << tag + @new_filtered_tags.uniq! end end end diff --git a/tests/acceptance_tests.rb b/tests/acceptance_tests.rb index b749e7d8..68f21003 100644 --- a/tests/acceptance_tests.rb +++ b/tests/acceptance_tests.rb @@ -127,6 +127,16 @@ describe 'signup' do page.must_have_content /Cannot have more than \d tags for your site/ end + it 'does not duplicate tags' do + fill_in_valid + fill_in 'tags', with: 'one, one' + click_button 'Create Home Page' + + site = Site.last + site.tags.length.must_equal 1 + site.tags.first.name.must_equal 'one' + end + it 'succeeds with no tags' do fill_in_valid fill_in 'tags', with: '' diff --git a/views/site.erb b/views/site.erb index 2f38219b..569dc3d4 100644 --- a/views/site.erb +++ b/views/site.erb @@ -1,4 +1,15 @@
+ <% flash.keys.each do |key| %> + <%= flash[key] %> + <% end %> +
+