ruby-on-rails - 如何通过单个控制器创建具有两个模型的两个记录(多步骤形式)
问题描述
我正在创建一个多步骤表单。但是,从提交的表格中,它将首先创建一个租户,用选定的单元更新租户,然后创建一个租约。
我尝试过 Wicked 但失败了,因为他们所有的东西都是为了“现有记录”而只是更新各个步骤的属性。
我已经使用了我自己的单控制器,然后按顺序更新。从下面的代码:如何从嵌套字段中提取/使用参数(有 2-3 层字段)和“创建”新记录。
class MoveInFlowsController < ApplicationController
def new
@tenant = Tenant.new
@address = Address.new
@unit = Unit.find(@app.unit_id)
@lease = Lease.new
@lease_move_in_charges = LeaseMoveInCharge.new
@lease_charges = LeaseCharge.new
end
def create
@tenant = Tenant.new(tenant_params)
##########################
# THIS IS THE PART I NEED HELP
############################
@lease = Lease.new(tenant_params[:lease_attributes])
# I NEED TO GET THE PARAMS FROM NESTED PARAMS BELOW.
if @tenant.save
@lease.tenant_id = @tenant.id
@lease.unit_id = @tenant.unit_id
if @lease.save
flash[:notice] = "Your move-in process completed successfully."
redirect_to(lease_path(:id => @lease.id))
else
render('new')
end
else
render('new')
end
end
private
def tenant_params
params.require(:tenant).permit(:first_name, :middle_ini, :last_name,
:mobile_phone, :work_phone, :home_phone, :email_address, :social_security,
:tenant_dob, :tenant_dl, :move_in_date, :send_rent_reminders, :primary_tenant,
:tenant_type, :emergency_contact_name, :emergency_contact_phone, :status, :unit_id, :property_id,
:co_tenant_first_name, :co_tenant_middle_ini, :co_tenant_last_name, :co_tenant_social_security,
:co_tenant_dob, :co_tenant_dl, :have_pets,
:rental_application_id, :unit_id,
address_attributes: [:address1, :address2, :city, :state, :zipcode, :_destroy],
notes_attributes: [:comment, :user_id],
lease_attributes:[:move_in_date,
:start_date,
:month_to_month,
:end_date,
:rent_due_day,
:grace_period,
:unit_id,
:tenant_id,
:base_late_fee,
:daily_late_fee,
:status,
lease_files: [],
lease_move_in_charges_attributes: [:amount, :gl_account,
:description, :_destroy],
lease_charges_attributes: [:amount, :gl_account,
:description, :_destroy]])
end
end
解决方案
我需要修复我的参数以读取:leases_attributes not lease_attributes 因为模型是用 has_many 设置的,并且接受嵌套属性。
推荐阅读
- angular - 根据屏幕大小移动 ng-content
- python - 如何在列表中存储信息
- flutter - 如何在 Flutter 上布局 Agora 视频屏幕?
- java - 如何在spring boot之上编写一个框架(即编写一个spring boot应用程序而不实现一些接口)
- python - 纸浆:设置不是基于 lpSum 而是在一个范围内的约束?
- python - 在 Python Windows 中成功冒充用户的步骤
- sql - Azure 流分析 - SQL
- python - 神经网络总是输出相同的值
- python - 在 Windows 机器上运行 TheHarvester 工具:找不到 python 模块
- python - 在python中绘制正交距离