我有一個簡單的模型的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