F.Error_Messages in Rails 3.0

f.error_messages in Rails 3.0

I just looked into the docrails github issues, and they've decided to remove f.error_messages instead of explaining how to do validation for comments.

DEPRECATION WARNING: f.error_messages was removed from Rails and is now available as a plugin

Thanks Michal. I will give you an upvote.
I found that the following was a good quick replacement for the upgrade:

  -if @link.errors.any?
%div#error_explanation
%h2
=pluralize(@link.errors.count, "error")+' '
prohibited this link from being saved:
%ul
-@link.errors.full_messages.each do |msg|
%li
=msg

DEPRECATION WARNING: f.error_messages was removed from Rails and is now available as a plugin

Thanks Michal. I will give you an upvote.
I found that the following was a good quick replacement for the upgrade:

  -if @link.errors.any?
%div#error_explanation
%h2
=pluralize(@link.errors.count, "error")+' '
prohibited this link from being saved:
%ul
-@link.errors.full_messages.each do |msg|
%li
=msg

Use error_messages in Rails 3.2? (raises undefined method error)

try the following code to iterate all errors, if any.

<%= form_for [@camp, @program] do |f| %>
<% @program.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<%= f.label :name %>
<% end %>

Rails error_messages helper

I found them here: error_messages have same params as error_messages_for.

In Rails 3 however, error_messages and error_messages_for have been deprecated and have been moved out to a plugin.

Rails errors NOT showing up in f.error_messages, but in errors they are

In this example f is not the @applicant object, it is the Form builder object. What you want is something like:

<%= error_messages_for 'applicant' %>

See the Rails API Docs.

validations for the model form

If I'm understanding your question correctly, you want to know how to validate the form on submission and how to display errors when there are some?

The validations you have in your model currently will validate the field "title" when you hit the submit button. So I think you're already good in that respect since that seems to be your only one.

To get errors to show up in your views, you want to use something like this is your form:

<%= form_for @pages do |f| %>
<h2><%= "#{pluralize(@pages.errors.count, "error")} prohibited this from being saved:" %></h2>
<% @pages.errors.full_messages.each do |msg| %>
<li><%= msg %></li>

<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<% end %>

This handles any errors in an elegant way. If there is more than one error, the pluralize method handles the pluralization of the word error so the sentence reads something like "2 errors have prohibited this from being saved." That way, should you decide to add more fields, you're already good to go.

Then pages.errors.full_messages displays each error in a sentence that is understandable to a user.

error_messages_for is deprecated in ruby - how do I find the replacement?

  • f.error_messages in Rails 3.0
  • Rails error_messages helper
  • http://www.emersonlackey.com/article/rails3-error-messages-for-replacement
  • http://ariejan.net/2010/12/15/why-did-errormessagesfor-disappear-from-rails-3/
  • https://gist.github.com/1113828

Just the top 5 links found searching Google for "rails error_messages_for rails 3".

That said, you really should use an up-to-date book. Even the latest free Rails tutorials are covering Rails 3.

form_for error messages in Ruby on Rails

Same as Rails 3 -- see f.error_messages in Rails 3.0 or http://railscasts.com/episodes/211-validations-in-rails-3 for many different possibilities.

My personal preference is to use simple_form and have it put the error next to the input.



Related Topics



Leave a reply



Submit