2010-08-04 75 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移除到單獨的插件中。