Why is error not showing on rhtml page


#1

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

<%= stylesheet_link_tag 'booksite', :media => "all" %> <% if flash[:notice] -%>

<%= flash[:notice] %>

<% end -%> <% form_for :chapter, :url => { :action => :add_chapter } do | form |%>

Title <%= form.text_field :title, :size => 40 %><%= error_message_on(:chapter, :title) %>

    <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 %>

Many thanks :slight_smile:


#2

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 :slight_smile: