Posts Tagged ‘ dynamic_form ’

f.error_messages in Rails 3.0

since I’m learning Ruby on Rails currently, and while running server on production mode with “rails server -e production”, i noticed that f.error_messages is deprecated with rails 3 and server pops up this error

ActionView::Template::Error (undefined method `error_messages’ for #<ActionView::Helpers::FormBuilder:0x905a7b8>):
1: <%= form_for (@project) do |f| %>
2: <%= f.error_messages %>
3: <p>
4: <%= f.label :name %>
5: <%= f.text_field :name %>

I spent a fair amount of time googling around this to find a solution and i ended up with this solution

<%= form_for (@project) do |f| %>
	 <% @project.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
		<%= f.label :name %>
		<%= f.text_field :name %>
	<%= f.submit %>
<% end %>

notice we used @project.errors.full_messages.each and looping through errors, rather than old method f.error_messages, though don’t forget to add

gem 'dynamic_form'

to Gemfile, hope this helps anyone facing this problem.

Happy Coding!