Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error messages in Ruby on Rails

I did a validates_presence_of fields to the comments in the guide. How to display validation errors on the posts page. I know about render, but with render i need to send all variables that i'm using on posts page. Am i right?

like image 227
Eugene Avatar asked Apr 08 '26 19:04

Eugene


2 Answers

I don't believe a plugin is required. The scaffold generator in Rails 3.x does it this way:

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

      <ul>
      <% @model.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<%# Rest of form... %>
like image 76
Fareesh Vijayarangam Avatar answered Apr 10 '26 08:04

Fareesh Vijayarangam


If you are looking to show the messages in your view, like so:

Rails UI for error_messages

This is what you want.. More info http://www.monochrome.co.uk/blog/2010/04/14/rails-3-error_messages-and-error_messages_for

`rails plugin install git://github.com/rails/dynamic_form.git`

<%= form_for @supermoon do |f| %>
  <%= f.error_messages %>

Plugin not required in rails 2.3 (is required in 3.x)

like image 34
Jesse Wolgamott Avatar answered Apr 10 '26 09:04

Jesse Wolgamott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!