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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

Parent

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

+1
−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.

History
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

Post
+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 .

History
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)
Oh, I should think of it as a *field* error rather than a *post* error. Thanks! I see an example th...
Monica Cellio‭ wrote over 1 year ago

Oh, I should think of it as a field error rather than a post error. Thanks! I see an example there that uses :base, which might be a way to attach the error to the higher-level object, but rather than introduce a variant approach into the code I'll adjust the message to say something like "Tags field is limited to 5 members" (I don't like "members" or "items" but will find something that fits within the framework here).

Taeir‭ wrote over 1 year ago

Indeed, :base is used to indicate "the object itself" (but no specific property thereof). I think some places of the code also put the error on base.

Monica Cellio‭ wrote over 1 year ago

I found some uses of :base for this, yes. Thanks again for the help; I submitted a PR.