【问题标题】:Rails not showing validation error messagesRails 不显示验证错误消息
【发布时间】:2010-08-04 08:07:03
【问题描述】:

大家好,我是 Rails 新手,刚开始填写第一份注册表单。我有一些验证需要 rails 检查,但由于某种原因它没有显示错误消息。

在 views/user/signup.html.erb 我有这个

<h1>Register Now!</h1>
<% form_for :user, @u, :url => { :action => "signup" } do |f| %>
    <%= f.error_messages :header_message => "Please Try Again!", :message => "We had some problems processing your registration" %>
    <%= f.label(:first, "First Name")%>
    <%= f.text_field(:first) %><br/>
    <%= f.label(:last, "Last Name")%>
    <%= f.text_field(:last) %><br/>
    <%= f.label(:username, "Username")%>
    <%= f.text_field(:username) %><br/>
    <%= f.label(:password, "Password")%>
    <%= f.password_field(:password) %><br/>
    <%= f.label(:password_confirmation, "Confirm Password")%>
    <%= f.password_field(:password_confirmation) %><br/>
    <%= f.label(:email, "E-mail")%>
    <%= f.text_field(:email) %> <br/>
    <%= f.label(:terms_of_service, "I agree to the terms of service") %>
    <%= f.check_box(:terms_of_service) %><br/>
    <%= f.submit("Sign Up")%>
<% end %>

在模型上我有以下内容

class User < ActiveRecord::Base
  validates_length_of :username, :within =>4..15
  validates_length_of :password, :within => 3..520
  validates_presence_of :first, :last, :username, :email, :password, :password_confirmation
  validates_uniqueness_of :username, :email
  validates_confirmation_of :password
  validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email"   
  validates_acceptance_of :terms_of_service, :message => "You must agree to our Terms of Service in order to create account"  
end

控制器

class UserController < ApplicationController
  def index
    @users = User.find(:all)
  end

  def signup
    if !params[:commit]
      render :action =>'signup'
    else
      @u = User.new
      @u.first = params[:first]
      @u.last = params[:last]
      @u.email = params[:email]
      @u.username = params[:username]
      @u.password = params[:password]
      @u.password_confirmation = params[:password_confirmation]
      @u.terms_of_service = params[:terms_of_service]
      if @u.valid?
        @u.password = Digest::SHA512.hexdigest(params[:password])
        @u.password_confirmation = Digest::SHA512.hexdigest(params[:password_confirmation])
        @u.save!
        render :action => 'success'
      else
        render :action => 'signup'
      end
    end
  end
end

编辑:由于某种原因我更新了我的代码,现在我无法创建用户。它会给我错误。 有谁知道如何解决这个问题?

【问题讨论】:

  • 在您的控制器中,您尝试保存的用户实例是否命名为“@user”?
  • jdeseno 是对的,您应该发布您的控制器代码或只是操作显示和注册

标签: ruby-on-rails validation


【解决方案1】:

问题可能是您在控制器中使用redirect 而不是render

错误消息不会在重定向后保留下来(重定向会导致您的浏览器立即请求新页面)。

如果你使用渲染,那么消息不会丢失。

【讨论】:

  • 由于您的回答,我意识到重定向时,闪光灯出现在重定向之后。重新渲染编辑页面时,我需要先闪烁,然后再渲染动作。
【解决方案2】:

假设您已将相关用户命名为控制器中的@user,请将表单更改为

<% form_for :user, @user, :url => { :action => "signup" } do |f| %>

这可能也有效

<% form_for @user, :url => { :action => "signup" } do |f| %>

然后在表格中进行相应的更改:

<%= f.label(:first, "First Name")%>
<%= f.text_field :first %>

所以加上 f。并删除每一个上的_tag。

以及错误消息部分:

<%= f.error_messages :header => "Please complete all the fields", :message => "There is a problem with one or more fields" %>

不确定最后一个中的 :header 和 :message 是如何工作的,但我希望你能明白。

更新了表单标签,因为它们有一个错误。

【讨论】:

  • 你现在可以只做&lt;% form_for @user do |f| %&gt;,如果资源已经正确设置,Rails 会处理剩下的事情。
  • 不知道。我从未使用过 error_messages_for -tag,但文档表明,error_messages_for 'user' 可能会代替 error_messages_for :user 工作(如果您将其命名为@user),但这是纯粹的猜测和对 api 文档的解释.rubyonrails.org.
【解决方案3】:

另一个问题可能是使用 rails 3 ,它将 error_messages 删除到单独的插件中。

【讨论】:

    猜你喜欢
    • 2022-07-07
    • 2022-06-20
    • 2016-11-08
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    相关资源
    最近更新 更多