Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7440759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:54:07+00:00 2026-05-29T10:54:07+00:00

I have got a form which is generated by model object. <%= form_for(@pages) do

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:54:07+00:00Added an answer on May 29, 2026 at 10:54 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a form which a user can use to create a new
I have my domain object, Client , I've got a form on my JSP
I have got a form that posts values to a page in a wizard.
We have got an extjs 3.1.1 form with file upload field ( http://www.extjs.com/deploy/dev/examples/form/file-upload.html from
I have got 2 DropDownList s on my Form and 1 GridView . I
I have got a textbox on a form with a method being called from
We have got a setup where users can either use a web-based form or
I've got a form where I have two radio buttons and two interchangeable controls
I've got a situation where I have a main form that pops up an
I have a nested form in which I would like the form fields to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.