Ruby on rails Rails::在嵌套模型形式中,更新属于;“家长”;属性
我正在构建一个Rails应用程序,但遇到了一个无法修复的表单问题。 我有一个Ruby on rails Rails::在嵌套模型形式中,更新属于;“家长”;属性,ruby-on-rails,nested,simple-form,belongs-to,Ruby On Rails,Nested,Simple Form,Belongs To,我正在构建一个Rails应用程序,但遇到了一个无法修复的表单问题。 我有一个order模型,它属于customer,所以当我构建表单时,我会@order=@customer.orders.build 这可以很好地保存订单属性,但我还想更新客户属性,这永远不会起作用 如何在同一流程中保存订单和更新“父”客户属性 谢谢你的帮助 编辑: 客户型号: class Customer < ActiveRecord::Base has_many :orders end class Order &l
order
模型,它属于customer
,所以当我构建表单时,我会@order=@customer.orders.build
这可以很好地保存订单属性,但我还想更新客户属性,这永远不会起作用
如何在同一流程中保存订单和更新“父”客户属性
谢谢你的帮助
编辑:
客户型号:
class Customer < ActiveRecord::Base
has_many :orders
end
class Order < ActiveRecord::Base
belongs_to :customer
accepts_nested_attributes_for :customer
end
class客户
订单型号:
class Customer < ActiveRecord::Base
has_many :orders
end
class Order < ActiveRecord::Base
belongs_to :customer
accepts_nested_attributes_for :customer
end
类顺序
我的项目:
- RubyonRails 4.2.6/Ruby2.2.2
- 设计3.5.9
- 简单表格3.1.0
def update
@customer = Customer.find(params[:id])
if @customer.update_attributes(customer_params.merge({ order_attributes: order_params}))
render @customer
end
end
其中,customer_params
和order_params
是使用强参数将表单发送的参数列入白名单的方法
显然,我还没有测试代码,但我应该给你一些指导
希望有帮助 您应该在创建方法中更新您的客户。那么:
def create
@order = current_customer.orders.build order_params
if @order.save
@order.customer.update_attributes(order_params[:customer_attributes])
...
else
...
end
end
更新客户属性意味着什么?您想将一些应该保存的属性发送到
客户
对象,或者您想根据订单设置一些属性
?客户有一个带有一些字段(姓名、地址、城市等)的配置文件。在订单中,有一部分客户可以更新他的一些信息。因此,当客户提交表单时,它应该创建订单并更新客户档案信息。