0

모델에 허용 된 매개 변수가 포함 된 기본 컨트롤러가 있습니다.[-1]을 사용하지 않고 중첩 된 강력한 매개 변수의 요소에 액세스하는 방법은 무엇입니까?

def order_params 
    allowed_attrs = [ 
    :id, 
    :first_name, 
    { 
     art_attributes: [ 
     :id 
     ], 
     item_attributes: [ 
     :id, 
     :art_id, 
     { 
      product_attributes: [ 
      :id, 
      :item_id 
      ] 
     } 
     ] 
    } 
    ] 


    if can? :manage, Order 
    allowed_attrs[-1][:art_job_attributes] += [ 
     :artist_id, 
     :ship_date 
    ] 
    end 

    params.require(:order).permit(allowed_attrs) 
end 

내 질문은 이것이다 : 다음 에서 할 수있는 경우에 여기에 단순화 된 버전의 모습입니까? 블록을 관리하려면 [-1]을 사용하지 않고 어떻게 art_job_attributes 요소에 액세스 할 수 있습니까? 내가 allowed_attrs의 하단을 수정하면 잠재적 버그가 될 것입니다. 너무 많은 시간이 소요될 수 있습니다처럼 동일한 문제와 루프에

.last

또는 .pop 결과는 같다.

allowed_attrs.index(:art_job_attributes) 

그러나이 전무 결과 : 이것이 내가 다음을 수행 할 수있는 배열은 기본적으로 있기 때문에

나는 생각했다.

도움 주셔서 감사합니다.

답변

0

이 문제에 대한 좋은 해결책은 아니지만 원하는대로 작동하도록했습니다. 나는 params를 헤어졌다. 최상위 변수를 분리 한 다음 메서드의 맨 아래에 결과를 구성했습니다. 다음은 내가 한 일입니다.

def order_params 

    allowed_attrs = [ 
    :id, 
    :first_name 
    ] 

    art_attrs = { 
    art_attributes:[ 
     :id 
    ], 
    item_attributes: [ 
     :id, 
     :art_id, 
     { 
     product_attributes: [ 
      :id, 
      :item_id 
     ] 
     } 
    ] 
    } 

    if can? :manage, Order 
    art_attrs[:art_attributes] += [ 
     :ship_date, 
     :artist_id 
    ] 
    end 

    allowed_attrs.push(art_attrs) 
    params.require(:order).permit(allowed_attrs) 
end