我正在创建一个多步骤表单。但是,它将从提交的表单中首先创建一个租户,使用选定的单位更新租户,然后创建一个租约。
我曾尝试过Wicked,但失败了,因为他们的所有东西都用于“现有记录”,只是在各个步骤上更新属性。
我求助于自己的Single Controller,然后按顺序更新。从以下代码中:如何从嵌套字段(有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
答案 0 :(得分:0)
我需要修正我的参数,使其读为:leases_attributes而不是lease_attributes,因为模型是使用has_many设置的,且具有接受嵌套的属性。