Simply Rails 2 - Chapter 6 - Pages 175-177

Hello All,

I am new to these forums, and to Rails…I’m not new to web development but I haven’t worked in it for a good 5 years or so. This book is quickly bringing me up to speed with everything I need to know to build a cutting edge web application.

My question lies on pages 175-177 of Chapter 6…specifically regarding the re-rendering of the form…when I look at the generated HTML of the rerendered form I do not have a FieldWithErrors div around my text field(s).

I feel like this is because of an error is the coding we do for the create method in the stories_controller…specifically, why can we use the if/else clause to check if @story saves, if we never actually tell @story to save, you know?

Maybe I am just completely missing something here.

Here is my code…i typed it at first, and when it wasn’t working i just went through and copied from the code archive, but it still doesn’t seem to work.

class StoriesController < ApplicationController
  def index
    @story = Story.find(:first, :order => 'RANDOM()')
  def new
    @story =
  def create
    @story =[:story])
      flash[:notice] = 'Story submission succeeded'
      redirect_to stories_path
      render :action => 'new'
class Story < ActiveRecord::Base
  validates_presence_of :name, :link
<% form_for @story do |f| %>
name: <br />
	<%= f.text_field :name %>
link: <br />
	<%= f.text_field :link %>
<%= submit_tag %>
<% end %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="xmllink**"
    xml:lang="en" lang="en">
    <meta http-equiv="Content-type" 
        content="text/html; charset=utf-8" />
    <%= stylesheet_link_tag 'style' %>
    <div id="content">
      <% unless flash[:notice].blank? %>
      	<div id="notification"><%= flash[:notice] %></div>
      <% end %>
      <%= yield %>

Does anyone know what I am doing wrong?

Thanks so much

Hi weckersham, welcome to the forums,

It looks like you missed this on page 177

Add the following line to the top of the new.html.erb template:

<%= error_messages_for 'story' %>

Did that work for you? I’ve added the error_mssages_for line to my new.html.erb file and I don’t have the div ‘fieldwitherrors’ wrapper on my form.

It ‘works’ - the .fieldwitherrors CSS I added to style.css makes the red box around the error, but I don’t get the html rendering when I view source after posting with the error.