在视图中看不到模型的验证消息错误

时间:2016-04-28 04:16:46

标签: ruby-on-rails activerecord

我可以在视图模板中显示模型的错误验证消息吗?

我的模特(截图)

My model

感谢!

1 个答案:

答案 0 :(得分:0)

将验证替换为:

validates :exp_name,  presence: {message: "your custom message"}
validates_length_of :exp_name, :minimum => 10, :maximum => 100, message: "your custom message"

将这些行放在视图中以显示错误:

<% if @experience.errors.any? %>
  <div>
    <h2><%= pluralize(@experience.errors.count, "error") %> prohibited this logo from being saved:</h2>
    <ul>
      <% @experience.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
       <% end %>
    </ul>
  </div>
<% end %>