[英]Rails not showing validation error messages
HI guys, I am new to rails and just doing my first registration form. 嗨,大家好,我是Rails的新手,只是做我的第一份注册表。 I have some validations that I want rails to check but for some reason it does not show the error messages. 我有一些要让Rails检查的验证,但是由于某种原因,它不显示错误消息。
On the views/user/signup.html.erb I have this 在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 %>
On the model I have the following 在模型上,我有以下内容
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
Controller 控制者
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
Edit: I updated my code for some reason now I cannot create a user. 编辑:我由于某种原因更新了代码,现在我无法创建用户。 it will give me errors. 这会给我错误。 Anyone knows how to get around this? 有人知道如何解决这个问题吗?
The problem could be that you are using redirect
rather than render
in your controller. 问题可能是您在控制器中使用redirect
而不是render
。
Error messages will not survive a redirect (a redirect causes your browser to immediately request a new page). 错误消息将无法在重定向中幸存(重定向会使您的浏览器立即请求新页面)。
If you use render then the messages will not be lost. 如果使用渲染,则消息不会丢失。
Assuming you've named the User in question as @user in the controller change the form to 假设您已在控制器中将有问题的用户命名为@user,则将表单更改为
<% form_for :user, @user, :url => { :action => "signup" } do |f| %>
This might work as well 这也可能工作
<% form_for @user, :url => { :action => "signup" } do |f| %>
Then in the form change accordingly: 然后在表格中进行相应的更改:
<%= f.label(:first, "First Name")%>
<%= f.text_field :first %>
So add f. 所以加f。 and remove _tag on each one. 并删除每个标签上的_tag。
And the errormessage part to: 错误消息部分包括:
<%= f.error_messages :header => "Please complete all the fields", :message => "There is a problem with one or more fields" %>
Not sure about how the :header and :message work in this last one, but I hope you get the idea. 不知道最后一个:header和:message是如何工作的,但是我希望您能理解。
UPDATEd the form tags, because they had a bug. 更新了表单标签,因为它们存在错误。
另一个问题可能是rails 3的使用,它会将error_messages删除到一个单独的插件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.