2010-08-04 83 views
1

嗨,大家好,我是新来的rails,只是做我的第一个注册表单。我有一些验证,我希望轨检查,但由于某种原因,它不显示错误消息。Rails没有显示验证错误信息

在视图/用户/ 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 

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

+0

在您的控制器中,您试图保存的用户实例名为'@user'? – jdeseno 2010-08-04 08:18:32

+0

jdeseno是正确的,你应该发布你的控制器代码或只是行动显示和注册 – Bohdan 2010-08-04 12:55:37

+0

没关系,我得到这个固定的 – denniss 2010-08-05 06:31:04

回答

7

假设你已经命名的问题作为控制器@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。

而且errormessage的部分:

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

不能确定如何:头和:在这最后一个消息的工作,但我希望你的想法。

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

+1

你可以''form_for @user do | f |如今,Rails将负责其余的部分,假设资源已经被正确设置。 – 2010-08-04 12:20:39

+0

我这样做的方式不起作用? – denniss 2010-08-05 05:41:04

+0

不知道。我从来没有使用error_messages_for-tag,但文档建议,error_messages_for'user'可能会工作,而不是error_messages_for:user(如果您已命名为@user),但这是纯粹的猜测和api文档的解释.rubyonrails.org。 – pkauko 2010-08-05 06:04:40

29

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

错误消息将无法通过重定向(重定向导致浏览器立即请求新页面)。

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

+0

谢谢指出,但我使用渲染。 – denniss 2010-08-05 05:40:48

+1

因为你的回答,我意识到,当重定向时,闪光来到重定向之后。重新渲染编辑页面时,我需要先闪烁,然后再渲染动作。 – Tass 2016-08-16 18:16:10

0

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