den*_*iss 1 validation ruby-on-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 %>
Run Code Online (Sandbox Code Playgroud)
在模型上我有以下内容
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
Run Code Online (Sandbox Code Playgroud)
调节器
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
Run Code Online (Sandbox Code Playgroud)
编辑:我出于某种原因更新了我的代码,现在我无法创建用户.它会给我错误.谁知道怎么解决这个问题?
Zac*_*ght 31
问题可能在于您使用redirect
而不是render
在控制器中.
错误消息将无法在重定向中生效(重定向会导致浏览器立即请求新页面).
如果使用渲染,则消息不会丢失.
假设您已将相关用户命名为控制器中的@user,请将表单更改为
<% form_for :user, @user, :url => { :action => "signup" } do |f| %>
Run Code Online (Sandbox Code Playgroud)
这可能也有效
<% form_for @user, :url => { :action => "signup" } do |f| %>
Run Code Online (Sandbox Code Playgroud)
然后在表格中相应地改变:
<%= f.label(:first, "First Name")%>
<%= f.text_field :first %>
Run Code Online (Sandbox Code Playgroud)
所以加上f.并删除每个_tag.
错误消息部分:
<%= f.error_messages :header => "Please complete all the fields", :message => "There is a problem with one or more fields" %>
Run Code Online (Sandbox Code Playgroud)
不确定:header和:message如何在最后一个中工作,但我希望你明白这个想法.
更新表单标签,因为他们有一个错误.
归档时间: |
|
查看次数: |
10247 次 |
最近记录: |