简体   繁体   中英

Validation not displaying error messages Ruby on Rails

I'm new to Ruby Rails and trying to follow along in the textbook, but my validation code isn't working. The original project was created by using the scaffold command:

rails generate scaffold Micropost content:text user_id:integer

Then the instructions tell me to add the following code to my Micropost model:

validates :content, length: { maximum: 140 }, presence: true

The validation works in that the form is not submitted if the length is longer than 140, but the problem is that there is no error message or anything. The button just does nothing. There is supposed to be a red error message, according to the textbook.

Any ideas?

It seems that your form does not show validation errors. Check out this answer https://stackoverflow.com/a/7880392/4077816 .

So I actually just discovered the problem a couple days ago. I was looking into this again for a Rails project I'm been working on, and discovered that in Rails 7 the Turbo Gem that is now included by default will only process render statements on the same controller if we add unprocessable_entity as the status.

So the validation was working, but the user couldn't see anything because the rendering was not working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM