Slightly updated version of the “crud” app on downloads page

November 14, 2008 | Filed Under Rails 

I wanted to note a detail about what happens when validation fails on model objects managed by a form; and how the form gets re-presented to the user.

The upshot of what I am about to describe is this: Make sure that all of the information the form needs is put back into the instance variables.

In the original version of the Student model, there were no validations - so a new Student would always get saved properly.

But let’s suppose that we add a validates_presence_of :last_name to the model. And let us suppose that the save fails because that field is left blank. Here’s what create looks like:


  def create
    @student = Student.new(params[:student])
    if @student.save
      redirect_to :action => :index
      flash[:notice] = 'Student was successfully created.'
    else
      render :action => :new
    end
  end

Now here’s the problem: We’ve posted back to create, and because the save doesn’t work, we go to the else, which asks to render with the template associated with the new action. (Remember, incidentally, that this doesn’t mean execute the new action — it means only: Use the template associated with new.

However, one thing that is required for that form to work is for the @sections instance variable to be set up. This is used to get the available sections to show in the drop-down. I would urge you to add the validation and try to save a student with an empty last name. See the error? So, what we need to do is find all of those sections before rendering the template associated with new. In short, it should look like this:


  def create
    @student = Student.new(params[:student])
    if @student.save
      redirect_to :action => :index
      flash[:notice] = 'Student was successfully created.'
    else
      @sections = Section.find(:all)   # Important line!
      render :action => :new
    end
  end

The previous version worked fine; but were we to have validations which result in the save failing, we would not be setting up the form properly.

I’ve uploaded a tweaked version of the “crud” app which you can get from the downloads page; both versions will work fine but I thought you should be aware of this important aspect of the form lifecycle.

Comments

Viewing 7 Comments

blog comments powered by Disqus