Rails: You need to supply at least one validation error

At first

** Day 21 of Calendar Planning 2020 ** It's been about 3 months since I started studying programming, so I will leave a note of what I learned in the article as an output. I would be happy if I could help anyone entering the world of programming. Please let me know if there are any words that are wrong, wrong, or misunderstood ^^ I'm sorry if it's hard to read the words for a long time. I will do my best to get used to it little by little.

The story of the error You need to supply at least one validation

I got this error when I created a custom validation. (It's my complete mistake ^^;)

environment

environment


Rails 5.2.4.4
ruby 2.6.3

Cause

demo.rb



validates :demo
validates :body, presence: true

def demo
  #Since it is not related to this case, it is omitted.
end

With this

スクリーンショット 2020-12-20 21.52.00.png

I get this error ^^;

Solution

demo.rb



validate :demo
validates :body, presence: true

def demo
  #Since it is not related to this case, it is omitted.
end

that's all!

It seems that it was validate instead of validates. The problem was that s was extra.

Standard validation is like validates. (If you use it often, the space is on the second line of the example that it is useless)

The custom validation seems to be validate.

Finally

I wrote a memo so that people who made the same mistake can solve it early. Please refer to it if you like.

Recommended Posts

Rails: You need to supply at least one validation error
If the custom validation method says "Argument Error: You need to supply at least one validation"
How to write Rails validation
[Rails] How to use validation
Why you need attr_accessor and why you don't (Thanks to Rails): Rails Tutorial Memorandum--Chapter 9
[Rails] Unexpected validation error in devise
Until you introduce fonts to Rails
To you who absolutely need backtrace
What to do if you get an error during rails db: reset
[Rails] How to display error messages individually
Basic Rails commands you want to learn