【发布时间】:2021-04-04 14:55:37
【问题描述】:
我正在使用 wicked gem 作为巫师形态。在我的表单的一个步骤中,我有上传附件的字段。我正在使用主动存储和 Rails 6。
在我看来
<%= f.file_field :plan %>
<%= f.file_field :appraisal %>
<%= f.file_field :flow %>
在我的 sale_steps 控制器中
def show
@sale = current_user.sales.find(params[:sale_id])
render_wizard
end
def update
@sale = current_user.sales.find(params[:sale_id])
params[:sale][:status] = step.to_s **(the error is on this line)**
@sale.update(sale_params)
render_wizard @sale
end
def sale_params
params.require(:sale).permit(.... :plan, :appraisal, :flow)
end
在我的模型中
has_one_attached :plan
has_one_attached :appraisal
has_one_attached :flow
validates :location, presence: true, if: -> { status?(:second_step) }
def status?(step_key)
status == step_key.to_s (this is to allow validations on each step - I have no validations defined for my attachments)
end
在我的表单中,当我到达向导的最后一步上传附件时,只要至少有一个附件存在,它就可以正常工作。但是,如果没有附件,我会在更新点击时收到以下无方法错误:
undefined method `[]=' for nil:NilClass
{"_method"=>"put", "authenticity_token"=>"[FILTERED]", "commit"=>"Continue", "sale_id"=>"37", "id"=>"fourth_step"}
我只是想知道为什么会这样(这与邪恶的宝石或其他什么有关?)有没有我可以尝试的解决方案?太棒了
【问题讨论】:
-
发生错误是因为您在
params[:sale][:status]中调用 []=,但params[:sale]为 nil,因为我认为它与任何 gem 无关,而是您如何修改来自paramsActionController::Parameters 对象的值。
标签: ruby-on-rails activerecord ruby-on-rails-6 wicked-gem