Ruby-on-rails 如何在 rails 视图上显示错误消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31092846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:16:16  来源:igfitidea点击:

How to show error message on rails views?

ruby-on-railsrubycontrollerviews

提问by Amit Pal

I am newbie in railsand want to apply validation on formfields.

我是新手,rails想对form字段应用验证。

myviewsnew.html.erb

myviewsnew.html.erb

<%= form_for :simulation, url: simulations_path do |f|  %>

<div class="form-group">
  <%= f.label :Row %>
  <div class="row">
    <div class="col-sm-2">
      <%= f.text_field :row, class: 'form-control' %>
    </div>
  </div>
</div>
.....

Simulation.rb

仿真.rb

class Simulation < ActiveRecord::Base
 belongs_to :user
 validates :row, :inclusion => { :in => 1..25, :message => 'The row must be between 1 and 25' }
end

simulation_controller.rb

模拟控制器.rb

class SimulationsController < ApplicationController

  def index
    @simulations = Simulation.all
  end

  def new
  end

  def create
    @simulation = Simulation.new(simulation_params)
    @simulation.save
    redirect_to @simulation
  end

  private
   def simulation_params
   params.require(:simulation).permit(:row)
  end

I want to check the integer range of rowfield in model class and return the error message if it's not in the range. I can check the range from above code but not able to return the error message

我想检查row模型类中字段的整数范围,如果不在范围内,则返回错误消息。我可以检查上述代码的范围,但无法返回错误消息

Thanks in advance

提前致谢

回答by Burak

The key is that you are using a model form, a form that displays the attributes for an instance of an ActiveRecord model. The create action of the controllerwill take care of some validation (and you can add more validation).

关键是您使用的是模型表单,该表单显示 ActiveRecord 模型实例的属性。控制器创建操作将处理一些验证(您可以添加更多验证)。

Controller re-renders newView when model fails to save

new模型保存失败时控制器重新渲染视图

Change your controller like below:

更改您的控制器,如下所示:

def new
  @simulation = Simulation.new
end

def create
  @simulation = Simulation.new(simulation_params)
  if @simulation.save
    redirect_to action: 'index'
  else
    render 'new'
  end
end

When the model instance fails to save (@simulation.savereturns false), then the newview is re-rendered.

当模型实例无法保存(@simulation.save返回false)时,new视图会重新渲染。

newView displays error messages from the model that failed to save

new视图显示来自未能保存的模型的错误消息

Then within your newview, if there exists an error, you can print them all like below.

然后在您的new视图中,如果存在错误,您可以将它们全部打印如下。

<%= form_for @simulation, as: :simulation, url: simulations_path do |f|  %>
  <% if @simulation.errors.any? %>
    <% @simulation.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <% end %>
  <% end %>
  <div class="form-group">
    <%= f.label :Row %>
    <div class="row">
      <div class="col-sm-2">
        <%= f.text_field :row, class: 'form-control' %>
      </div>
    </div>
  </div>
<% end %>

The important part here is that you're checking whether the model instance has any errors and then printing them out:

这里的重要部分是您正在检查模型实例是否有任何错误,然后将它们打印出来:

<% if @simulation.errors.any? %>
  <%= @simulation.errors.full_messages %>
<% end %>

回答by Amit Suroliya

Do this -

做这个 -

 <%= form_for :simulation, url: simulations_path do |f|  %>
    <% if f.object.errors.any? %>
      <ul>
        <% if f.object.errors.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    <% end %>

   ..........
 <% end %> 

回答by Emilio González Monta?a

You just need to add this code to the view file (myviewsnew.html.erb):

您只需将此代码添加到视图文件 ( myviewsnew.html.erb) 中:

<%= error_messages_for :simulation %>

Check complete syntax of error_messages_forin http://apidock.com/rails/ActionView/Helpers/ActiveRecordHelper/error_messages_for

检查的完整语法error_messages_forhttp://apidock.com/rails/ActionView/Helpers/ActiveRecordHelper/error_messages_for