0

I have got a form which is generated by model object.

<%= form_for(@pages) do |f| %>

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

Here is the controller method for this:

def new
    @pages = Page.new

    respond_to do |format|
    format.html  # new.html.erb
    format.json  { render :json => @post }
    end
  end

Here is the model code:

class Page < ActiveRecord::Base
validates :title, :presence => true
end

Now, how can I validate the form on submission.(i know that submit button is not there, i will add it later). I have used <%= f.error_messages %> in the form but it is giving me error :

NoMethodError in Pages#new

Showing C:/rorapp/app/views/pages/_form.html.erb where line #2 raised:

undefined method `error_messages' for #<ActionView::Helpers::FormBuilder:0x49b9ca8>
Extracted source (around line #2):

1: <%= form_for(@pages) do |f| %>
2:   <%= f.error_messages %>
3:   <p>
4:     <%= f.label :title %><br />
5:     <%= f.text_field :title %>
Trace of template inclusion: app/views/pages/new.html.erb

Rails.root: C:/rorapp

Application Trace | Framework Trace | Full Trace
app/views/pages/_form.html.erb:2:in `block in _app_views_pages__form_html_erb__975660997_39217440'
app/views/pages/_form.html.erb:1:in `_app_views_pages__form_html_erb__975660997_39217440'
app/views/pages/new.html.erb:2:in `_app_views_pages_new_html_erb___256256638_47476836'
app/controllers/pages_controller.rb:11:in `new'
Request

Parameters:

{"title"=>"",
 "author"=>"",
 "email"=>"",
 "body"=>"",
 "reference"=>"Google"}
Show session dump

Show env dump

Response

Headers:

None

How can I validate it?

Maverick
  • 2,738
  • 24
  • 91
  • 157

2 Answers2

2

My Rails experience is mostly with version 2.3.14, so I can't be 100% sure about this, but it looks like f.error_messages was depreciated in Rails version 3.0

Does this previous question help? f.error_messages in Rails 3.0

Edit: basically what I'm saying is it looks like you're trying to display errors the old way, which is probably incompatible with your Rails version. The link I posted above has a few suggestions.

Community
  • 1
  • 1
DRobinson
  • 4,441
  • 22
  • 31
1

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.

Phil Aquilina
  • 919
  • 1
  • 7
  • 17