diff --git a/app/models/category.rb b/app/models/category.rb index 971d5d0fb58..d52a6941d25 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -85,6 +85,11 @@ class Category < ActiveRecord::Base if name.present? self.slug = Slug.for(name) + # Reject slugs that only contain numbers, because that's indistinguishable from an id. + self.slug = '' unless self.slug =~ /[^\d]/ + + return if self.slug.blank? + # If a category with that slug already exists, set the slug to nil so the category can be found # another way. category = Category.where(slug: self.slug) diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index cb4f937bee8..0c3f8d12e6b 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -104,6 +104,14 @@ describe Category do end end + describe 'slug would be a number' do + let(:category) { Fabricate(:category, name: "電車男 2") } + + it 'creates a blank slug' do + category.slug.should be_blank + end + end + describe 'after create' do before do @category = Fabricate(:category, name: 'Amazing Category')