P
PerfectDayToChaseTornados
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_onchapter, :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[revious_chapter_id]) if
params[revious_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[arent_id] = params[arent_id] if params[arent_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_onchapter, :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[arent_id] -%>
<%= hidden_field_tag revious_chapter_id, flash[arent_id],
:size => 40 %>
<% end -%>
<%= submit_tag "Submit Chapter", :class => "submit" %>
<% end %>
</body>
</html>
Many thanks
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_onchapter, :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[revious_chapter_id]) if
params[revious_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[arent_id] = params[arent_id] if params[arent_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_onchapter, :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[arent_id] -%>
<%= hidden_field_tag revious_chapter_id, flash[arent_id],
:size => 40 %>
<% end -%>
<%= submit_tag "Submit Chapter", :class => "submit" %>
<% end %>
</body>
</html>
Many thanks