首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用相同的控制器方法调用不同参数的ruby

用相同的控制器方法调用不同参数的ruby
EN

Stack Overflow用户
提问于 2022-10-27 22:47:22
回答 1查看 39关注 0票数 0

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

代码语言:javascript
复制
  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方法时,我没有得到这些参数。任何帮助都是值得赞赏的。

EN

回答 1

Stack Overflow用户

发布于 2022-11-09 02:32:20

我认为你最好把inspection_request.save放在不同的方法上

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74229024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档