简体   繁体   English

验证不显示错误消息 Ruby on Rails

[英]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.我是 Ruby Rails 的新手,并试图按照教科书进行操作,但我的验证代码不起作用。 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:然后说明告诉我将以下代码添加到我的 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.验证的工作原理是如果长度超过 140,则不提交表单,但问题是没有错误消息或任何内容。 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 .看看这个答案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.我正在为我正在处理的 Rails 项目再次调查这个问题,并发现在 Rails 7 中,如果我们添加 unprocessable_entity 作为状态,现在默认包含的 Turbo Gem 只会处理相同 controller 上的渲染语句。

So the validation was working, but the user couldn't see anything because the rendering was not working.所以验证工作正常,但用户看不到任何东西,因为渲染不工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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