Serialized form fields in Ruby on Rails problem

Posted by Violet on Stack Overflow See other posts from Stack Overflow or by Violet
Published on 2010-06-10T00:39:58Z Indexed on 2010/06/10 0:42 UTC
Read the original article Hit count: 311

I'm having a problem making serialized columns in my model persist correctly in forms. If my model validation fails I want to redisplay the "new" page with all my model data still in the forms. Right now, everything except the serialized fields seem to persist (if my Order fails to purchase, on the "new" page the email is still filled in but the shipping address fields are not). Is this a Rails bug or am I doing something wrong?

My model:

class Order < ActiveRecord::Base
    serialize :shipping_address
end

My controller:

 def new
   @order = Order.new
 end

def create
  @order = Order.new params[:order]
  if @order.purchase then render :action => "success"
  else render :action => "new"
  end      
end

My view, new.html.haml:

= form_for @order do |f|
   - if @order.errors.any?
  #errorExplanation
    %p The following errors occurred:
    %ul
      - for msg in @order.errors.full_messages
        %li= msg
  %h2 Billing Information
  = f.label :email
  = f.text_field :email
  %h2 Shipping Address
  = f.fields_for :shipping_address do |b|
    %p.field.address
      = b.label :address1
      = b.text_field :address1
 %p= f.submit "Place Order"

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about forms