[英]Failure when re-rendering _form.html.erb after failing validation in Ruby on Rails 5
My application manages Business Rules, which apply to one business object. 我的应用程序管理业务规则,该规则适用于一个业务对象。 When editing a business rule, I meet an unattended failure.
在编辑业务规则时,我遇到了无人值守的失败。
The _form.html.erb view correctly displays current properties, including the dropdown field : _form.html.erb视图正确显示当前属性,包括下拉字段:
<%= f.collection_select :business_object_id, @business_objects_list, :id, :name %>
Current value is displayed, available values are shown in the drop down list. 显示当前值,可用值显示在下拉列表中。
When applying a modification (not related to this drop down) which fails validation, Rails tries to render _form.html.erb, but raises an error due to missing @business_objects_list: 当应用验证失败的修改(与此下拉列表无关)时,Rails尝试呈现_form.html.erb,但由于缺少@business_objects_list而引发错误:
undefined method `map' for nil:NilClass
nil:NilClass的未定义方法“ map”
@business_objects_list is created in the private section of the business rules controller: @business_objects_list在业务规则控制器的专用部分中创建:
# Retrieve business objects list
def set_business_objects_list
if action_name == 'edit'
my_business_area = @business_rule.business_process.business_flow.business_area_id
else
my_business_area = BusinessProcess.find(params[:business_process_id]).business_flow.business_area_id
end
@business_objects_list = BusinessObject.where("business_area_id = ?", my_business_area)
end
The set_business_objects_list function is called at the top of the controller: 在控制器顶部调用set_business_objects_list函数:
before_action :set_business_objects_list, only: [:new, :edit]
EDIT: Controller actions 编辑:控制器动作
# GET /business_rules/1/edit
def edit
### Retrieved by Callback function
end
# PUT /business_rules/1
# PUT /business_rules/1.json
def update
### Retrieved by Callback function
@business_rule.updated_by = current_user.user_name
respond_to do |format|
if @business_rule.update_attributes(business_rule_params)
format.html { redirect_to @business_rule, notice: 'Business rule was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @business_rule.errors, status: :unprocessable_entity }
end
end
end
Can you please help find out what's wrong here? 您能帮忙找出这里有什么问题吗? Thanks!
谢谢!
undefined method `map' for nil:NilClass
nil:NilClass的未定义方法“ map”
You need to make @business_objects_list
available to the update
action as well because render action: "edit"
just loads the edit.html.erb
. 您还需要使
@business_objects_list
可用于update
操作,因为render action: "edit"
仅加载edit.html.erb
。 So the scope of @business_objects_list
is lost 因此,
@business_objects_list
的范围丢失了
Solution: 解:
Just add the update
action to the list of actions to the before_action :set_business_objects_list
只需将
update
操作添加到before_action :set_business_objects_list
的操作列表中before_action :set_business_objects_list
before_action :set_business_objects_list, only: [:new, :edit, :update]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.