简体   繁体   English

验证是否存在无法在Rails 2.3.8中工作的情况

[英]Validates presence of not working in Rails 2.3.8

I have a simple Rails app that has a model that is contained in several places. 我有一个简单的Rails应用,该应用的模型包含在多个地方。 I allow the updating of the model from several different controllers in Rails 2.3.8. 我允许从Rails 2.3.8中的几个不同的控制器更新模型。 In my model I have code that allows for the name and the description to be validated. 在我的模型中,我有允许验证名称和描述的代码。 If they are blank Active_Scaffold should be populating the div with an error message. 如果它们为空,则Active_Scaffold应该在div中填充一条错误消息。 This works in the page that is linked directly to the User's controller but if I include the user anywhere else it will only show up on the User's controller page and not on the page that they are currently on. 这在直接链接到用户控制器的页面中有效,但是如果我在其他任何地方包括该用户,它将仅显示在用户控制器页面上,而不显示在他们当前所在的页面上。 Shouldn't Acitive_Scaffold magically redirect to the correct place? Acitive_Scaffold是否应该神奇地重定向到正确的位置?

Code: 码:

class User < ActiveRecord::Base
    validates_presence_of :name, :description, :allow_blank => false

    def create_from_params(params)
        @name = params[:name]
        @description = params[:description]
    end

As you can see nothing spectacularly hard. 如您所见,没有什么特别困难。

EDIT: Sorry I left out the rest of the method signature 编辑:对不起,我遗漏了方法签名的其余部分

I suspect the active_scaffold views only check for errors on the model it represents. 我怀疑active_scaffold视图仅检查它代表的模型上的错误。 Add this error check code to any view but the user view. 将此错误检查代码添加到用户视图以外的任何视图中。

<%= error_messages_for :user %>

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

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