如何离开填充fields_for的字段?

时间:2016-01-07 17:57:44

标签: ruby-on-rails

我遇到了一个问题:当客户在我的表单中输入他的送货和结算信息时,如果重新加载页面,地址模型的字段将丢失所有信息。 它适用于直接包含在订单模型中的其他字段,如电子邮件。重装后如何让田地填满?

这是我的new.html.erb文件:

<%= form_for @order do |f| %>
  <ul>
   <% @order.errors.each_with_index do |msg, i| %>
    <li><%= msg[1] %></li>
   <% end %>
  </ul>
 <%= f.text_field :email, placeholder: "email" %>
 <%= f.fields_for :shipping_address_attributes do |sa| %> 
  <%= sa.text_field :first_name, placeholder: "Firstname" %>  
  <%= sa.text_field :last_name, placeholder: "Name", autocomplete: true %>
  <%= sa.text_field :address1, placeholder: "Address", autocomplete: true %>
  #etc.
 <% end %>
 <p><%= f.submit 'Next step' %></p>
<% end %>

我的模特:

class Order < ActiveRecord::Base
 belongs_to :billing_address, :class_name => "Address"
 belongs_to :shipping_address, :class_name => "Address"
 accepts_nested_attributes_for :shipping_address
 accepts_nested_attributes_for :billing_address, reject_if: :bill_to_shipping_address    

class Address < ActiveRecord::Base
  belongs_to :user
  has_many :billing_addresses, :class_name => "Order", :foreign_key => "billing_address_id", dependent: :nullify
  has_many :shipping_addresses, :class_name => "Order", :foreign_key => "shipping_address_id", dependent: :nullify
  validates_presence_of :first_name, :last_name, :address1, :city, :country, :phone, :postal

和我的order_controller.rb:

def new
 @user = current_user
 @order_items = current_order.order_items
 @order = current_order
 @amount = @order.subtotal
end

def update
@order_items = current_order.order_items
@order = current_order
  if @order.update(order_params)
    redirect_to recapitulatif_de_la_commande_path
  else
    redirect_to(:back)
  end
end

编辑

可能错误是由于订单在更新操作之前未与任何送货地址相关联的事实?仅当地址模型验证每个属性的存在时,它才有效。

有什么想法吗?

0 个答案:

没有答案