Rails 应用中未生成表单错误消息
Form Error Messages Not Generating in Rails App
您好,我正在构建一个简单的应用程序,当有人在字段中输入无效信息或根本没有输入任何信息时,无法显示错误消息。
我使用的表格是注册一个新用户,与用户和表格关联的代码如下;
users_controller.rb
Class UsersController < ApplicationController
def index
@users = User.all
end
def show
@user = User.find(params[:id])
@country = Country.all
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to @user
else
redirect_to '/signup'
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password)
end
end
user.rb
class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :first_name, presence: true, length: { maximum: 25 }
validates :first_name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
has_many :trips
has_many :countries, through: :trips
end
new.html.erb
<div class="container">
<h1 class="text-center">Sign up</h1>
<div class="row">
<div class="col-md-6 offset-md-3 ">
<%=form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: "form-control" %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: "form-control" %>
<%= f.label :email %>
<%= f.email_field :email, class: "form-control" %>
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
<%= f.submit "Create an account", class: 'form-control btn btn-primary' %>
<% end %>
</div>
</div>
</div>
_error_messages.html.erb
<% if @user.errors.any? %>
<div class="alert alert-danger">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% else %>
<h3>test</h3>
<% end %>
当我加载表单时,我确实看到了 "Test" 字符串,我将其放入 _error_messages.html.erb 以提高可见性。但是,当我在注册页面中输入数据时,它会重新加载页面(当所有字段都有效时,它应该重新加载而不是将其发送到用户页面)。但是,"Test" 字符串仍然出现在顶部,而不是应该出现的错误消息。
我的假设是我需要某种会话或某种东西来记住错误是什么,因为现在它会重新加载一个全新的页面,内存中没有任何内容,但是,我目前无法在任何地方找到解决方案.
如有任何帮助,我们将不胜感激!
正如我所说,你需要改变
redirect_to '/signup'
到
render 'new'
来自Guides
The render
method is used so that the @user
object is passed back
to the new
template when it is rendered. This rendering is done within
the same request as the form submission, whereas the redirect_to
will
tell the browser to issue another request.
也就是说,当 redirect_to
向浏览器发出 新请求 时, @user
的值将丢失,换句话说 @user
是重新实例化的新实例。这就是为什么 <% if @user.errors.any? %>
总是 returns false
就好像 @user
.
没有错误一样
您好,我正在构建一个简单的应用程序,当有人在字段中输入无效信息或根本没有输入任何信息时,无法显示错误消息。
我使用的表格是注册一个新用户,与用户和表格关联的代码如下;
users_controller.rb
Class UsersController < ApplicationController
def index
@users = User.all
end
def show
@user = User.find(params[:id])
@country = Country.all
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to @user
else
redirect_to '/signup'
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password)
end
end
user.rb
class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :first_name, presence: true, length: { maximum: 25 }
validates :first_name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
has_many :trips
has_many :countries, through: :trips
end
new.html.erb
<div class="container">
<h1 class="text-center">Sign up</h1>
<div class="row">
<div class="col-md-6 offset-md-3 ">
<%=form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: "form-control" %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: "form-control" %>
<%= f.label :email %>
<%= f.email_field :email, class: "form-control" %>
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
<%= f.submit "Create an account", class: 'form-control btn btn-primary' %>
<% end %>
</div>
</div>
</div>
_error_messages.html.erb
<% if @user.errors.any? %>
<div class="alert alert-danger">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% else %>
<h3>test</h3>
<% end %>
当我加载表单时,我确实看到了 "Test" 字符串,我将其放入 _error_messages.html.erb 以提高可见性。但是,当我在注册页面中输入数据时,它会重新加载页面(当所有字段都有效时,它应该重新加载而不是将其发送到用户页面)。但是,"Test" 字符串仍然出现在顶部,而不是应该出现的错误消息。
我的假设是我需要某种会话或某种东西来记住错误是什么,因为现在它会重新加载一个全新的页面,内存中没有任何内容,但是,我目前无法在任何地方找到解决方案.
如有任何帮助,我们将不胜感激!
正如我所说,你需要改变
redirect_to '/signup'
到
render 'new'
来自Guides
The
render
method is used so that the@user
object is passed back to thenew
template when it is rendered. This rendering is done within the same request as the form submission, whereas theredirect_to
will tell the browser to issue another request.
也就是说,当 redirect_to
向浏览器发出 新请求 时, @user
的值将丢失,换句话说 @user
是重新实例化的新实例。这就是为什么 <% if @user.errors.any? %>
总是 returns false
就好像 @user
.