Forum: Ruby why is error not showing on rhtml page

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
PerfectDayToChaseTornados (Guest)
on 2007-03-30 00:43
(Received via mailing list)
Hi All,

Would someone please be kind enough to help? I think I've got everything
right (though obviously I havent). I am putting some validation in my
model
class & expecting the error to show on the page, however it isnt. When I
submit the page with a title value, my 'created chapter' message appears
&
the chapter is persisted. If I submit without the title, validation
fails
(as it should), the 'something went wrong dude' message is shown. But
the
'error_message_on(:chapter, :title)' is not shown on the page. I've
tried
several things, but to no avail & I'm now tearing my (already thinning)
hair
out! Code follows.

chapter.rb has validates_presence_of :title

chapter_controller.rb
class ChapterController < ApplicationController

  def add_chapter
    if request.post?
      chapter = Chapter.new(params[:chapter])
      previous_chapter = Chapter.find(params[:previous_chapter_id]) if
params[:previous_chapter_id]
      chapter.parent = previous_chapter if previous_chapter
      chapter.is_actual = false

      if chapter.save
        flash[:notice] = "created chapter #{chapter.title}"
        redirect_to :action => 'add_chapter'
      else
        flash.now[:notice] = "something went wrong dude!"
      end

    end

    flash[:parent_id] = params[:parent_id] if params[:parent_id]
  end

end

add_chapter.rhtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<%= stylesheet_link_tag 'booksite', :media => "all" %>
</head>
<body>
    <% if flash[:notice] -%>
        <h1><%= flash[:notice] %></h1>
    <% end -%>
    <% form_for :chapter, :url => { :action => :add_chapter } do | form
|%>
        <p>
            <label for="title">Title</label>
            <%= form.text_field :title, :size => 40 %><b><%=
error_message_on(:chapter, :title) %></b>
        </p>


        <p>
            <label for="synopsis">Synopsis</label>
            <%= form.text_area :synopsis, :rows => 20, :cols => 60 %>
        </p>

        <p>
            <label for="content">Content</label>
            <%= form.text_area :content, :rows => 30, :cols => 60 %>
        </p>
        <% if flash[:parent_id] -%>
             <%= hidden_field_tag :previous_chapter_id,
flash[:parent_id],
:size => 40 %>
        <% end -%>

        <%= submit_tag "Submit Chapter", :class => "submit" %>

    <% end %>

</body>
</html>

Many thanks :-)
PerfectDayToChaseTornados (Guest)
on 2007-03-30 16:56
(Received via mailing list)
Where I was refereing to chapter in my controller I should have been
refering to @chapter, d'oh! Coming from Java to Ruby, I need to get used
to
this :-)
This topic is locked and can not be replied to.