簡體   English   中英

Rails沒有顯示驗證錯誤消息

[英]Rails not showing validation error messages

嗨,大家好,我是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

編輯:我由於某種原因更新了代碼,現在我無法創建用戶。 這會給我錯誤。 有人知道如何解決這個問題嗎?

問題可能是您在控制器中使用redirect而不是render

錯誤消息將無法在重定向中幸存(重定向會使您的瀏覽器立即請求新頁面)。

如果使用渲染,則消息不會丟失。

假設您已在控制器中將有問題的用戶命名為@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是如何工作的,但是我希望您能理解。

更新了表單標簽,因為它們存在錯誤。

另一個問題可能是rails 3的使用,它會將error_messages刪除到一個單獨的插件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM