在 Rails 6.1 应用程序中,我已将新操作从单独的页面移动到模式,现在我试图弄清楚是否可以只显示错误消息(如果它存在,例如“名称不能是空白") 直接在表单中,而不是重新加载整个页面。因为即使在这种情况下,我也不知道如何直接重新加载表单并显示错误消息。
def create
@list = List.new(list_params)
if @list.save
redirect_to list_path(@list)
else
redirect_to root_path
end
end
这是使用 simple_form 创建的模态中存在的表单
<%= simple_form_for(@list) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs my-3">
<%= f.input :name, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
另一种解决方案是使用闪光灯,但如果错误消息直接显示在表单中而不重新加载整个页面,我会更高兴。
如果有帮助,这是我目前的项目