Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 显示来自rails模型的验证错误消息_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby 显示来自rails模型的验证错误消息

Ruby 显示来自rails模型的验证错误消息,ruby,ruby-on-rails-3,Ruby,Ruby On Rails 3,是否可以通过使用我们的方法来显示来自控制器的验证错误消息?请检查下面的代码 validate :validation def validation if self.RJan.nil? && self.RFeb.nil? && self.RMar.nil? && self.R1.nil? #How do write my error message here ? end end 还有我的表格 <% if @

是否可以通过使用我们的方法来显示来自控制器的验证错误消息?请检查下面的代码

validate :validation
  def validation
    if self.RJan.nil? && self.RFeb.nil? && self.RMar.nil? && self.R1.nil?
      #How do write my error message here ?
    end
  end
还有我的表格

<% if @record.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@record.errors.count, "error") %> prohibited this record from being saved:</h2>

      <ul>
      <% @record.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

禁止保存此记录:

您可以使用

self.errors.add(:base, "your message here")
您可以将属性名称作为符号,而不是
:base
或任何您喜欢的内容

就你而言

if self.RJan.nil? && self.RFeb.nil? && self.RMar.nil? && self.R1.nil?
  self.errors.add(:base, "your message here")
end

因此,在if块的模型中,我应该写入self.errors.full_消息?显然有些地方出错了,它没有显示任何验证的错误消息!我回答了你的另一个问题。上面的代码只是为了向实例添加新错误。与视图或显示机制无关