ruby-on-rails 使用不同的参数调用相同的控制器方法ruby

n1bvdmb6  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(159)

我有一个私有和第三方检查。在自动创建私有检查记录后,我需要创建第三方检查。因此,我必须使用不同的参数再次从控制器调用create_history方法

class HomesController < BaseController
    
    def create_history
      #<ActionController::Parameters {"request_type"=>"Private", "user_id"=>"25" "requested_at"=>"Oct 28, 2022","private_inspection_type_id"=>"23780"} permitted: true>
     inspection_request = @home.inspection_requests.new(inspection_request_history_params)
     inspection_request.save
     if inspection_request.get_paired_inspection.present?
       inspection_request_history_params =  {request_type: "Third Party", third_party_inspection_id: inspection_request.get_paired_inspection.id, status: inspection_request.status, user_id: inspection_request.user_id, requested_at: inspection_request.requested_at }
       create_history
     end
    end
    
   def inspection_request_history_params
     params.require(:inspection_request).permit(:request_type, :user_id, :requested_at, :performed_at, :private_inspection_type_id, :third_party_inspection_id)
    end

当我尝试通过传递不同的参数来调用create_history方法时,我没有得到参数。

c9qzyr3d

c9qzyr3d1#

我觉得把inspection_request.save放在不同的方法上比较好

class HomesController < BaseController    
  def create_history
    inspection_request = create_inspection_requests(inspection_request_history_params)

    if inspection_request.get_paired_inspection.present?
      create_inspection_requests({
        request_type: "Third Party", 
        third_party_inspection_id: inspection_request.get_paired_inspection.id, 
        status: inspection_request.status, 
        user_id: inspection_request.user_id, 
        requested_at: inspection_request.requested_at 
      })
    end
  end

  private

  def create_inspection_requests(attributes)
    inspection_request = @home.inspection_requests.new(attributes)
    inspection_request.save

    return inspection_request
  end
  
  def inspection_request_history_params
    params.require(:inspection_request).permit(:request_type, :user_id, :requested_at, :performed_at, :private_inspection_type_id, :third_party_inspection_id)
  end
end

相关问题