2017-02-06 10 views
0

Ruby on Rails에 Solidus 전자 상거래 보석을 사용하고 있습니다. 이메일을 보내기위한 전자 상거래 웹 사이트를 만들고 있으므로 사용자의 청구서 수신 주소 및 배송 주소가 아닌 개별 광고 항목에 주소를 연결할 수 있어야합니다. 이것을 달성하기 위해, 나는 장바구니 형태로 주소 매개 변수를 전달합니다.Allowed_attributes가있는 강력한 매개 변수 (Solidus)

나는 Unpermitted parameter: address_attributes을 얻고 있습니다.

매개 변수

Parameters: {"utf8"=>"✓", "authenticity_token"=>"[AUTHENTICITY_TOKEN]", "content"=>"", 
"order"=>{"address_attributes"=>[{"firstname"=>"", "lastname"=>"", "address1"=>"", "address2"=>"", "city"=>"", "country_id"=>"232", "state_id"=>"", "zipcode"=>"", "phone"=>""}]}, "variant_id"=>"1", "quantity"=>"1", "button"=>""} 
여기

내가 그들에게 접근하고있어 방법은 다음과 같습니다 : address_attributes = order_params[:address_attributes] 여기에 내가 내 양식을 통해 통과하고있어 매개 변수입니다.

orders_controller.rb

def order_params 
    if params[:order] 
    params[:order].permit(*permitted_order_attributes) 
    else 
    {} 
    end 
end 

(내가 대신 params.require의 경우 다른 논리를 사용하는 이유를 잘 모르겠지만, 그것의 : 여기

은 상선과 함께 제공되는 order_params 방법입니다 . 나의 코드) 내가 거기에 디버거를 넣어 permitted_order_attributes 이것 좀 봐 내가 볼 것입니다 :

[:coupon_code, :email, :special_instructions, :use_billing, {:bill_address_attributes=>[:id, :firstname, :lastname, :first_name, :last_name, :address1, :address2, :city, :country_id, :state_id, :zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company, {:country=>[:iso, :name, :iso3, :iso_name], :state=>[:name, :abbr]}], :ship_address_attributes=>[:id, :firstname, :lastname, :first_name, :last_name, :address1, :address2, :city, :country_id, :state_id, :zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company, {:country=>[:iso, :name, :iso3, :iso_name], :state=>[:name, :abbr]}], :payments_attributes=>[:amount, :payment_method_id, :payment_method, {:source_attributes=>[:number, :month, :year, :expiry, :verification_value, :first_name, :last_name, :cc_type, :gateway_customer_profile_id, :gateway_payment_profile_id, :last_digits, :name, :encrypted_data, :existing_card_id, 
    { 
    :address_attributes=>[ 
     :id, :firstname, :lastname, :first_name, :last_name, :address1, :address2, :city, :country_id, :state_id, :zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company, {:country=>[:iso, :name, :iso3, :iso_name], :state=>[:name, :abbr]} 
    ] 
    } 
]}], :shipments_attributes=>[:special_instructions, :stock_location_id, :id, :tracking, :selected_shipping_rate_id]}, {:line_items_attributes=>[:id, :variant_id, :quantity]}] 

그래서 address_attributes가 있습니다. 이상한 점은 {}에 싸여 있다는 것입니다. 내 이해는 그것이 같은 모양의 매개 변수를 찾고 있다는 것을 의미합니다 : "order"=>{["address_attributes"=>{"firstname"=>" ... 등. 그러나 나는 긍정적이지 않다. Solidus가 그런 식으로 전달할 매개 변수를 찾는 이유를 모르겠습니다. 그런 식으로 매개 변수를 전달하는 방법을 모르겠습니다. 나는이에 주문 PARAMS 방법을 변경하는 경우

_cart_form.html.erb

<%= f.fields_for('address_attributes[][]', @address) do |form| %> 
    <%= render :partial => 'spree/address/form', :locals => { form: form, 
     address_type: 'shipping', address: @address } %> 
    <% end %> 

작업

주위 :

다음
def order_params 
    params.require(:order).permit(:address_attributes=>[:id, :firstname, :lastname, :first_name, :last_name, :address1, :address2, :city, :country_id, :state_id, :zipcode, :phone, :state_name, :country_iso, :alternative_phone, :company, {:country=>[:iso, :name, :iso3, :iso_name], :state=>[:name, :abbr]}]) 
end 

내 양식을 여기에 내 양식입니다 공장. 그래서, 해결 방법이 있지만 여기에 무슨 일이 일어나는지 알고 싶습니다. 이 문제를 해결하는 방법은 몇 가지가 있습니다.

  1. 매개 변수를 기존 order_params 기대치를 충족시키는 형태로 전달하십시오. .
  2. 수정 permitted_order_attributes 내가 그들을 통과하고있어 나는 당신이 내가 사용 싶어하는 JSON 포맷 도구로 permitted_order_attributes에 대한지고 어떤 덤프

답변

1

매개 변수를 승인하고이 이동 :

[ 
    : coupon_code, 
    : email, 
    : special_instructions, 
    : use_billing, 
    { 
    : bill_address_attributes=>[ 
     : id, 
     : firstname, 
     : lastname, 
     : first_name, 
     : last_name, 
     : address1, 
     : address2, 
     : city, 
     : country_id, 
     : state_id, 
     : zipcode, 
     : phone, 
     : state_name, 
     : country_iso, 
     : alternative_phone, 
     : company, 
     { 
     : country=>[ 
      : iso, 
      : name, 
      : iso3, 
      : iso_name 
     ], 
     : state=>[ 
      : name, 
      : abbr 
     ] 
     } 
    ], 
    : ship_address_attributes=>[ 
     : id, 
     : firstname, 
     : lastname, 
     : first_name, 
     : last_name, 
     : address1, 
     : address2, 
     : city, 
     : country_id, 
     : state_id, 
     : zipcode, 
     : phone, 
     : state_name, 
     : country_iso, 
     : alternative_phone, 
     : company, 
     { 
     : country=>[ 
      : iso, 
      : name, 
      : iso3, 
      : iso_name 
     ], 
     : state=>[ 
      : name, 
      : abbr 
     ] 
     } 
    ], 
    : payments_attributes=>[ 
     : amount, 
     : payment_method_id, 
     : payment_method, 
     { 
     : source_attributes=>[ 
      : number, 
      : month, 
      : year, 
      : expiry, 
      : verification_value, 
      : first_name, 
      : last_name, 
      : cc_type, 
      : gateway_customer_profile_id, 
      : gateway_payment_profile_id, 
      : last_digits, 
      : name, 
      : encrypted_data, 
      : existing_card_id, 
      { 
      : address_attributes=>[ 
       : id, 
       : firstname, 
       : lastname, 
       : first_name, 
       : last_name, 
       : address1, 
       : address2, 
       : city, 
       : country_id, 
       : state_id, 
       : zipcode, 
       : phone, 
       : state_name, 
       : country_iso, 
       : alternative_phone, 
       : company, 
       { 
       : country=>[ 
        : iso, 
        : name, 
        : iso3, 
        : iso_name 
       ], 
       : state=>[ 
        : name, 
        : abbr 
       ] 
       } 
      ] 
      } 
     ] 
     } 
    ], 
    : shipments_attributes=>[ 
     : special_instructions, 
     : stock_location_id, 
     : id, 
     : tracking, 
     : selected_shipping_rate_id 
    ] 
    }, 
    { 
    : line_items_attributes=>[ 
     : id, 
     : variant_id, 
     : quantity 
    ] 
    } 
] 

마치 address_attributes의 유일한 발생이 payment_attributes>source_attributes 안에 중첩되어있는 것처럼 보입니다. 이것은 원하는 구조가 어딘지 깨진 것처럼 들립니다.

당신은 당신의 보호 PARAMS에 대한 자신의 제안 코드를 무시하는 것이 더 수 있으며, 명시 적으로 일반적인 방법으로 당신이 원하는 구조화 : 당신이 원하는 당신의 통제에서 100 % 것

def order_params 
    params.require(:order).permit(:your, :choice, :of, :fields, 
           address_attributes: [:address, :fields], 
           other_nested_thing_attributes: [:stuff, :here]) 
end 

귀하의 양식을 수락하십시오. 또한 양식 구조의 오류를 강조 표시하는 데 도움이됩니다.

HTH - 의견이나 질문.

+0

아, 그 말이 맞습니다! 그래, 나는 가능한 한 Solidus에서 기존 인프라를 납치하려하고있다. address_attributes는 특정 방식으로 사용되도록 설계되었습니다. 내 해결 방법과 같은 소리는 어딘가에 허용되는 속성을 정의해야하지만 갈 길입니다. 감사합니다 :) –

+0

@ WylliamJudd 허용 된 속성에 대한 order_params는 어디에 두었습니까? – Mink

+0

@Mink - ActiveRecord 객체에 값을 대량 할당 할 때 (@ order.new (order_params) 나 @ order.update_attributes (order_params)와 같이) 컨트롤러에서 사용되는 것으로 생각됩니다. –