开发者

Serialized form fields in Ruby on Rails problem

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"


I add the same problem today, it appears the form isn't properly built in the view. I simply added at the beginning of my create action:

 params[:order][:shipping_address] = params[:shipping_address]

And it works properly

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜