我有一个简单的模型的Rails应用程序:Rails的4.0:保存HAS_ONE协会不起作用
客户HAS_ONE地址
地址belongs_to的客户
new.html.erb
<%=f .fields_for :address do |a| %>
<%=a .label :street %>
<br>
<%=a .text_field :street %>
<br>
<%=a .label :number %>
<br>
<%=a .text_field :number %>
<br>
<%=a .label :zipcode %>
<br>
<%=a .text_field :zipcode %>
<br>
<%=a .label :city %>
<br>
<%=a .text_field :city %>
<br>
<%=a .label :country %>
<br>
<%=a .text_field :country %>
<br>
<% end %>
customer_controller.rb
def create
@customer = Customer.new(customer_params)
@customer.save
redirect_to @customer
end
private
def customer_params
params.require(:customer).permit(:ctype, :name, :dateOfBirth, :image, :custom1, :custom2, :custom3, :email, :phone, :mobilphone, :website, address_attributes:[:street, :number, :zipcode, :city, :country])
end
如果我创建一个新的客户和地址我的地址不会被保存:(
你能可以帮助我吗?
你在模型中使用'accep_nested_attributes_for'吗? – 2014-10-01 09:39:04
运行'@ customer.save!'有什么例外? – 2014-10-01 09:39:24
nope我不使用accept_nested_attributes_for:/不听我添加之前,我需要耙:回滚 - >耙db:迁移??,我没有得到一个例外... – 2014-10-01 09:53:22