zeromodulus
zeromodulus

Reputation: 73

How do I re-populate form fields when validation fails?

This is the erb template:

<div id='recipe-form'>
  <% if @recipe.errors %>
    <div id='errors'>
      <% @recipe.errors.messages.each do |field, messages| %>
        <div class='error'>
          <div class=field'><%= field %></div>
          <div class='messages'>
            <ul>
            <% messages.each do |message| %>
              <li><%= message %></li>
            <% end %>
            </ul>
          </div>
        </div>
      <% end %>
    </div>
  <% end %>
  <%= form_for @recipe, :html => {:multipart => true}, :url => '/recipes'  do |f| %>

    <%= f.label :title, 'title' %>
    <%= f.text_field :title %>

    <div id="photo-upload">
      <%= file_field :photo0, :image, :id => 0 %>
    </div>

    <div id='existing-photos'>
      <% recipe.photos.each do |photo| %>
        <div id='<%= photo.id %>'>
          <img src='<%= photo.image.url(:thumb) %>' />
          <ul>
            <li>
              <%= link_to 'delete',
                    recipe_photo_url(
                      :recipe_id => @recipe.slug,
                      :id => photo.id
                    ),
                    :method => :delete,
                    :remote => true
              %>
            </li>
          </ul>
        </div>
      <% end %>
    </div>

    <%= f.label :body, 'body' %>
    <%= f.cktext_area :body, :ckeditor => {:width => "500"} %>

    <%= f.label :tags, 'tags (comma separated)' %>
    <%= text_field_tag :tags %>

    <%= submit_tag 'submit' %>
  <% end %>
</div>

This is the create action:

def create
  @recipe = Recipe.new(params[:recipe])

  photo_keys = params.keys.select{|k|k.match(/^photo/)}
  @photos = []
  photo_keys.each do |photo_key|
    @photos << Photo.new(params[photo_key])
  end

  @recipe.tags = Tag.parse(params[:tags])

  @recipe.author = current_user

  if @recipe.save &&
       @photos.all?{|photo|photo.save}
    @photos.each do |photo|
      photo.recipe_id = @recipe.id
      photo.save
    end
    flash[:notice] = 'Recipe was successfully created.'
    redirect_to recipe_url(@recipe.slug)
  else
    flash[:error] = 'Could not create recipe. '
    flash[:error] += 'Please correct any mistakes below.'
    render :action => :new
  end
end

And this is the new action:

def new
  @recipe = Recipe.new
end

I read that if I use form_for as I am using above, the fields will be re-populated automatically.

When I inspect @recipe.errors from within the erb template, I can see that the errors generated by create are also available when the new action is rendered, but the fields do not re-populate.

Upvotes: 2

Views: 1121

Answers (1)

Ismael
Ismael

Reputation: 16730

I'm actually not sure about what render action: does but what I do and works is: Instead of rendering the action just render the template using render :new.

You need to set the same instance variables (those with @), which you already in your create action.

Upvotes: 2

Related Questions