Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Notifications
Mark all as read
Q&A

How should I fix this incorrect error message (don't quite understand the code) Question

+0
−0

I saw this report about a bug in an error message and figured that would be an easy fix. The incorrect error message is: "Tags can't have more than 5 tags". That should be "Posts can't have...". I figured I'd need to find and edit a string.

I grepped the code and found my way to app/models/post.rb, which includes this:

  def maximum_tags
    if tags_cache.length > 5
      errors.add(:tags, "can't have more than 5 tags")
    elsif tags_cache.empty?
      errors.add(:tags, 'must have at least one tag')
    end
  end

What is :tags here? I don't see anything in this file that defines it, so it must be coming from somewhere else. (Have I mentioned I'm very much a newbie with Ruby and Rails?)

I think the logic here is that the error is being caused by tags validation, so we're adding it to a set of errors about that field. But we don't want to use the name of the field in the error message here.

I realize it's possible to hack around this problem by changing the string here to a sentence that can legitimately begin with the word "Tags", but I'd rather understand what the correct change would be.

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+1
−0

Rails has a whole validation logic for showing nice errors whenever things are not valid. What you are adding an error to is the tags "field" of "Post" (in app/models/post.rb you will see a has_many :tags). Internally, rails also uses this information when you would programmatically check "which part of this post is invalid now?". You would ideally want that to answer to remain as "tags".

Perhaps the rails guide on validations can provide more information on alternative solutions: https://guides.rubyonrails.org/active_record_validations.html .

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Oh, I should think of it as a *field* error rather than a *post* error. Thanks! I see an example th... (3 comments)

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate