开发者

how to custom error message

I am trying custom some error message

validates :username, :presence      => {:message => 'Email cannot be blank'}, 
                     :uniqueness    => {:message => 'Email was existed'}

but in the web page, it dis开发者_开发知识库play like this:

Username Email cannot be blank
Username Invalid email format
Password Missing password

How can i get rid of the "Username" and "Password" in the very first word of each line??

(rails 3.0.7, ruby 1.9.2)

Thanks


Replace the code in the view that is displaying the errors with something like one of the examples below.

If you are using HAML:

- if @applicant.errors.any?
  %ul
  - @applicant.errors.each do |key, value|
    %li= value

If you are using ERB: (the rails default template engine)

<% if @applicant.errors.any? %>
  <ul>
    <% @applicant.errors.each do |key, value| %>
      <li><%= value %></li>
    <% end %>
  </ul>
<% end %>

Obviously there is a lot that can be done to style it, but I left that out for simplicity.

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜