我在我的参数中得到了这个:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wdOkjrI8no6xcooBaNithP1KtVNvwvwyJL1vcEuFTSo=", "complete"=>"not completed", "commit"=>"Save Data", "task_form"=>{"rent_balance"=>"$900.00", "misc_balance"=>"$0.00", "total_balance"=>"$900.00", "date_of_petition"=>"", "nop_court"=>"Select", "index_number"=>""}, "id"=>"1059"}
如果nop_court
的值为nil
,我必须将'Select'
的值更新为if params[:task_form][:nop_court] == "Select"
@task.update_data_item_values(@task_form_attributes)
@task.update_data_item_values(params[:task_form][:nop_court] => " ")
else
@task.update_data_item_values(@task_form_attributes)
end
。
为此,写了以下内容:
params[:task_form][:nop_court]
在 def update_data_item_values(task_form_attributes)
#list of data items for task
@task_data_items = self.data_items
if task_form_attributes
#updating value for each data item
task_form_attributes.keys.each do |task_data_item_name|
task_data_item = @task_data_items.select { |task_data_item| task_data_item.data_item_name == task_data_item_name}.first
value = task_form_attributes[task_data_item_name]
if task_data_item.present?
if task_data_item.data_item_data_type == "date"
if value.present?
formatted_value = task_data_item.blank? ? Date.strptime(value, '%m-%d-%Y').strftime("%Y-%m-%d") : value
#formatted_value = value.to_datetime.strftime('%m-%d-%Y') unless value.blank?
data_item_value = formatted_value
end
else
data_item_value = value
end
end
task_data_item.update_attributes(:data_item_value => data_item_value) if task_data_item.present?
end
end
end
我得到值select,所以上面的代码不起作用。我怎样才能得到参数nop_court
名为update_data_item_values的方法内的代码如下:
{{1}}
答案 0 :(得分:3)
在你的if语句之前,我假设你将@task_form_attributes指定为:
@task_form_attributes = params[:task_form]
在调用update_data_item_values之前尝试更新此变量:
if params[:task_form][:nop_court] == "Select"
@task_form_attributes[:nop_court] = " "
end
@task.update_data_item_values(@task_form_attributes)