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()')
  end
  
  def new
    @story = Story.new
  end
  
  def create
    @story = Story.new(params[:story])
    if @story.save
      flash[:notice] = 'Story submission succeeded'
      redirect_to stories_path
    else
      render :action => 'new'
    end
  end
end
class Story < ActiveRecord::Base
  validates_presence_of :name, :link
end
<% form_for @story do |f| %>
<p>
name: <br />
	<%= f.text_field :name %>
</p>
<p>
link: <br />
	<%= f.text_field :link %>
</p>
<p>
<%= submit_tag %>
</p>
<% end %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "anotherlink">
<html xmlns="xmllink**"
    xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-type" 
        content="text/html; charset=utf-8" />
    <title>Shovell</title>
    <%= stylesheet_link_tag 'style' %>
  </head>
  <body>
    <div id="content">
      <h1>Shovell</h1>
      <% unless flash[:notice].blank? %>
      	<div id="notification"><%= flash[:notice] %></div>
      <% end %>
      <%= yield %>
    </div>
  </body>
</html>

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.