Tuesday, 15 April 2014

ruby on rails - How to set minimum and maximum number of tags? -



ruby on rails - How to set minimum and maximum number of tags? -

i using acts-as-taggable-on gem , trying create sort of validation on number of tags. followed tutorial , works fine: http://railscasts.com/episodes/382-tagging

i have article acts taggable. validate tags need nowadays , number of characters:

class article < activerecord::base acts_as_taggable validates :tag_list, presence: true, length: { maximum: 100 } end

this not solution tags there many of them many different sizes , separated commas. how can set required minimum , maximum number of tags need connected article? set minimum 3 tags , maximum 10 tags length of text not solution.

thank help :)

this illustration should point right direction:

validate :tag_list_validation def tag_list_validation errors[:tag_list] << "3 tags minimum" if tag_list.count < 3 errors[:tag_list] << "10 tags maximum" if tag_list.count > 10 self.tag_list.each |tag| errors[:tag_list] << "#{tag} must shorter 100 characters maximum" if tag.length > 100 end end

edit: solution question:

class article < activerecord::base acts_as_taggable validate :tag_list_count def tag_list_count errors[:tag_list] << "3 tags minimum" if tag_list.count < 3 errors[:tag_list] << "10 tags maximum" if tag_list.count > 10 end end

ruby-on-rails ruby-on-rails-4 acts-as-taggable-on

No comments:

Post a Comment