You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails表单验证错误在ERB中不显示,但开发者工具预览可见

我正在尝试在Rails表单中展示验证错误,然而这些错误仅能在开发者工具的“Preview”中看到,却无法在应用页面本身显示(见截图)。我查了很多资料,还是没找到问题原因,相关代码如下:

models/user.rb
# == Schema Information
#
# Table name: users
#
#  id              :bigint           not null, primary key
#  first_name      :string
#  last_name       :string
#  email           :string
#  password_digest :string
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#
class User < ApplicationRecord
  has_secure_password

  validates :first_name, :last_name, :email, presence: true
  validates_uniqueness_of :email
end
controllers/users_controller.rb
class UsersController < ApplicationController
  def new
    @user = User.new
  end
  
  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to dashboard_index, notice: "Thanks for signing up!"
    else
      render :new
    end
  end

  private
  def user_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
  end
end
views/users/new.html.erb
<h1>Sign Up</h1>
<%= form_with(model: @user, local: true) do |f| %>
  <%= @user.errors.inspect %>
  <p><%= @user.errors.any? %></p>
  <% if @user.errors.any? %>
    <div>
      <ul>
        <% @user.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name %>
  </div>
  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions"><%= f.submit "Sign Up" %></div>
<% end %>
截图

错误仅在开发者工具Preview中显示,页面DOM中不显示

内容的提问来源于stack exchange,提问作者twk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 20:33:26