rails 视图上的模型验证错误无法正确呈现
rails model validation error on view does not render properly
我在表单上使用状态 radio_button 字段,当我在表单上添加 validate :status, presence: true
错误消息时,无法正确呈现它使表单视图变得奇怪。有什么解决方案可以更好地呈现状态错误吗?
我已经添加了屏幕截图,看看它是内联的状态字段,但在错误之后我得到了打开其他状态字段。
<div class="row">
<div class="field columns large-6">
<%= form.label :status %>
<span>
<%= form.radio_button :status, "1" %>
<%= form.label :completed %>
</span>
<span>
<%= form.radio_button :status, "0" %>
<%= form.label :inprogess %>
</span>
<span>
<%= form.radio_button :status, "2" %>
<%= form.label :yet_to_start %>
</span>
</div>
<div class="field columns large-6"></div>
</div>
试试这个,将以下代码添加到您的 application.rb
文件
config.action_view.field_error_proc = Proc.new { |html_tag, instance| html_tag }
或者您可以尝试通过添加 css
使字段内联
.fields_with_error{ display: inline }
它将停止用 div 包装有错误的字段,这可能会解决您的问题。值得一试。
我在表单上使用状态 radio_button 字段,当我在表单上添加 validate :status, presence: true
错误消息时,无法正确呈现它使表单视图变得奇怪。有什么解决方案可以更好地呈现状态错误吗?
我已经添加了屏幕截图,看看它是内联的状态字段,但在错误之后我得到了打开其他状态字段。
<div class="row">
<div class="field columns large-6">
<%= form.label :status %>
<span>
<%= form.radio_button :status, "1" %>
<%= form.label :completed %>
</span>
<span>
<%= form.radio_button :status, "0" %>
<%= form.label :inprogess %>
</span>
<span>
<%= form.radio_button :status, "2" %>
<%= form.label :yet_to_start %>
</span>
</div>
<div class="field columns large-6"></div>
</div>
试试这个,将以下代码添加到您的 application.rb
文件
config.action_view.field_error_proc = Proc.new { |html_tag, instance| html_tag }
或者您可以尝试通过添加 css
使字段内联.fields_with_error{ display: inline }
它将停止用 div 包装有错误的字段,这可能会解决您的问题。值得一试。