Home>

It is rails 5.2.

The rating column of the Review model is
・ You can enter from 1 to 5
・ Required input
Is the rule.

So I set it as follows.

class Review</pre>
<p>When rating is not entered, the following is displayed on the screen.<br />
In this case, I want to display only "Enter Rating".</p>
<pre><code>There are two input errors
Please enter the Rating numerically
Please enter a Rating

I also tried the following, but it only changed the order of the error messages.

validates: rating, presence: true,
  numericality: {
    less_than_or_equal_to: 5,
    greater_than_or_equal_to: 1
 }

How would you describe it?
Please tell me your seniors. Thanking you in advance.

  • Answer # 1

    validates: rating, numericality: {
          less_than_or_equal_to: 5,
          greater_than_or_equal_to: 1
       },
       if: Proc.new {| a | a.rating.present?}