ruby-on-rails - 启动创建操作时,会话变量不持久 Rails 6
问题描述
我正在处理一个需要向表单对象发送 id 的表单,然后使用我传递的 Id 创建该表单。我在会话变量中启动了对象的 id,表单初始化没有问题。但是,它在创建操作期间失败。好像我从未分配过对象的 id....如何在表单处理创建操作时保留 id?
链接到表单操作
<%= link_to 'New Contract', new_provider_contract_path(provider_form_id: provider.id), class: 'remove-text-decoration button' %>
# GET /provider_contracts/new
def new
@provider = ProviderForm.find(params[:provider_form_id])
session[:provider_form_id] = @provider
@provider_contract = @provider.provider_contracts.build(provider_form: session[:provider_form_id])
@provider_contract.build_contract_profile
@provider_contract.build_contract_information
@provider_contract.build_contract_term
@provider_contract.build_contract_additional_term
@provider_contract.contract_comments.build
rescue ActiveRecord::RecordNotFound
redirect_to provider_contracts_path, notice: 'The provider has been removed or has not been selected'
end
# POST /provider_contracts
# POST /provider_contracts.json
def create
@provider_contract = ProviderContract.new(provider_contract_params)
respond_to do |format|
if @provider_contract.save
format.html { redirect_to @provider_contract, notice: 'Provider contract was successfully created.' }
format.json { render :show, status: :created, location: @provider_contract }
else
format.html { render :new }
format.json { render json: @provider_contract.errors, status: :unprocessable_entity }
end
end
end
provider_contracts_controller
# Never trust parameters from the scary internet, only allow the white list through.
def provider_contract_params
params.require(:provider_contract).permit( :provider_form_id,
ProviderContract.attribute_names.map(&:to_sym),
ProviderForm.attribute_names.map(&:to_sym),
contract_to_facility_connections_attributes: ContractToFacilityConnection.attribute_names.map(&:to_sym).push(:id, :_destroy),
contract_reward_attributes: ContractReward.attribute_names.map(&:to_sym).push(:id, :_destroy),
contract_profile_attributes: ContractProfile.attribute_names.map(&:to_sym).push(:id, :_destroy, :contract_document),
contract_information_attributes: ContractInformation.attribute_names.map(&:to_sym).push(:id, :_destroy),
contract_term_attributes: ContractTerm.attribute_names.map(&:to_sym).push(:id, :_destroy),
contract_term_fees_attributes: ContractTermFee.attribute_names.map(&:to_sym).push(:id, :_destroy),
contract_term_stipends_attributes: ContractTermStipend.attribute_names.map(&:to_sym).push(:id, :_destroy),
contract_additional_term_attributes: ContractAdditionalTerm.attribute_names.map(&:to_sym).push(:id, :_destroy),
contract_term_call_types_attributes: ContractTermCallType.attribute_names.map(&:to_sym).push(:id, :_destroy),
contract_comments_attributes: ContractComment.attribute_names.map(&:to_sym).push(:id, :_destroy, user: current_user))
end
表格错误
ProviderForm must exist!
路线.rb
resources :provider_contracts
完整的日志
Started POST "/provider_contracts" for 127.0.0.1 at 2019-11-14 14:44:39 -0500
Processing by ProviderContractsController#create as HTML
Parameters: {"authenticity_token"=>"Pm0kHge1RXSVDwZV9X7AsSam0B+EHjIfwHwBfXrJ9Dn2fp5to5u9RCa5xsA==", "provider_contract"=>{"contract_to_facility_connections_attributes"=>{"1573760656135"=>{"facility_id"=>"2", "_destroy"=>"false"}}, "status"=>"LOCUM", "is_confidential"=>"", "contract_profile_attributes"=>{"contract_file_type"=>"CV", "provider_specialty"=>"", "contract_class"=>"", "engaget"=>"", "comment"=>""}, "contract_information_attributes"=>{"contract_stage"=>"Submitted for Approval", "commencement_date(1i)"=>"2019", "commencement_date(2i)"=>"11", "commencement_date(3i)"=>"14", "assigned_to"=>"", "is_commencement_date_unknown"=>"0", "process_owner"=>"", "existing_or_returning"=>"", "action_requested"=>"", "background_check_status"=>"", "reason_for_contract_request"=>"", "watermark"=>"", "deadline_date(1i)"=>"2019", "deadline_date(2i)"=>"11", "deadline_date(3i)"=>"14", "priority"=>""}, "contract_term_attributes"=>{"frequency_status"=>"Daily", "compensation_amount"=>"", "time_off_status"=>"", "optional_time_off"=>"", "optional_time_off_count"=>"", "optional_time_off_days"=>"", "contracted_hours_amount"=>"", "overtime_excess"=>""}, "contract_additional_term_attributes"=>{"malpractice_provider"=>"Employer/Company", "malpractice_cost"=>"", "malpractice_limits"=>"", "health_insurance_eligibility"=>"", "health_insurance_cost"=>"", "workers_comp"=>"", "initial_term_amount"=>"", "initial_term_time_length"=>"", "renewal_term_amount"=>"", "renewal_term_time_length"=>"", "termination_notice_amount"=>"", "termination_notice_time_length"=>"", "non_renewal_notice_amount"=>"", "renewal_notice_time_length"=>"", "comment"=>""}, "provider_form"=>"1"}, "commit"=>"Save Contract"}
User Load (2.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/provider_contracts_controller.rb:95:in `provider_contract_params'
Unpermitted parameter: :provider_form
(0.5ms) BEGIN
↳ app/controllers/provider_contracts_controller.rb:40:in `block in create'
Facility Load (0.7ms) SELECT "facilities".* FROM "facilities" WHERE "facilities"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/controllers/provider_contracts_controller.rb:40:in `block in create'
(0.6ms) ROLLBACK
↳ app/controllers/provider_contracts_controller.rb:40:in `block in create'
Rendering provider_contracts/new.html.erb within layouts/application
Facility Load (1.0ms) SELECT "facilities".* FROM "facilities"
↳ app/views/provider_contracts/_contract_to_facility_connection_fields.html.erb:2
Rendered provider_contracts/_contract_to_facility_connection_fields.html.erb (Duration: 37.7ms | Allocations: 4269)
CACHE Facility Load (0.1ms) SELECT "facilities".* FROM "facilities"
↳ app/views/provider_contracts/_contract_to_facility_connection_fields.html.erb:2
Rendered provider_contracts/_contract_to_facility_connection_fields.html.erb (Duration: 63.3ms | Allocations: 4026)
Rendered provider_contracts/partials/_contract_profile.html.erb (Duration: 141.8ms | Allocations: 10810)
Rendered provider_contracts/partials/_contract_information.html.erb (Duration: 113.1ms | Allocations: 15419)
Rendered provider_contracts/partials/_contract_term.html.erb (Duration: 105.4ms | Allocations: 12074)
Rendered provider_contracts/_contract_term_fee_fields.html.erb (Duration: 74.0ms | Allocations: 9916)
Rendered provider_contracts/_contract_reward_fields.html.erb (Duration: 74.1ms | Allocations: 9886)
Rendered provider_contracts/_contract_term_stipend_fields.html.erb (Duration: 46.5ms | Allocations: 11007)
Rendered provider_contracts/_contract_term_call_type_fields.html.erb (Duration: 50.2ms | Allocations: 13248)
Rendered provider_contracts/_contract_additional_term.html.erb (Duration: 48.9ms | Allocations: 14852)
Rendered provider_contracts/_form.html.erb (Duration: 920.8ms | Allocations: 112376)
Rendered provider_contracts/new.html.erb within layouts/application (Duration: 921.3ms | Allocations: 112442)
Rendered navbars/_user_navbar.html.erb (Duration: 2.1ms | Allocations: 745)
Rendered flash_messages/_messages.html.erb (Duration: 0.1ms | Allocations: 21)
Completed 200 OK in 1022ms (Views: 988.4ms | ActiveRecord: 4.9ms | Allocations: 157626)
解决方案
这只是一个猜测,但它不能是@provider.id
你的形式provider_form_id: provider.id
吗?<%= link_to 'New Contract', new_provider_contract_path(provider_form_id: provider.id), class: 'remove-text-decoration button' %>
你也可能想添加
def set_provider
@provider = ProviderForm.find(params[:provider_form_id])
end
在您的 provider_contracts_controller.rb 中的 allowed_params 的私有部分,并在控制器的最开始调用before_action :set_provider, only: %i[create]
如果您还没有在控制器中使用它,那么值得一试。
推荐阅读
- php - 如何调用这种 php 对象
- c++ - 具有快速查找功能的 C++ 列表
- javascript - 将 PHP 变量传递给 javascript 时出现空警报(回声有效)
- c# - 如何在 c# 控制台中像 webbrowser 一样加载网站?
- sql - 如何在 SQL 中访问 JSON 对象的字段?
- python - python中的多处理库无法导入自定义模块,并且在同一python文件中定义函数时也不起作用
- nginx - 如何在 nginx 入口添加自定义安全策略和引用策略
- python - 如何在 Django 视图中进行 url 编码
- python - 从 tf.keras.applications 将正则化器添加到模型时无法保存模型
- php - 错误:调用未定义的函数 Tests\factory()