Rails中深层嵌套的参数不会将值从表单更改为控制器

时间:2019-10-11 21:57:00

标签: ruby-on-rails activerecord

我有一个来自红宝石的怪异错误。我不知道这是bug还是什么,但是在报告之前,我只是想确定一下。

我有一个多态表,属于2个模型。

多态表

module Spree
  class AccountPrice < Spree::Base
    validates :accountable, presence: true

    belongs_to :accountable, polymorphic: true
    belongs_to :variant_price_set, class_name: "Spree::VariantPriceSet"
    belongs_to :shipping_method_price_set, class_name: "Spree::ShippingMethodPriceSet"

    accepts_nested_attributes_for :variant_price_set, :shipping_method_price_set

  end
end

我的CorporateAccount可以包含许多这些多态表。

class Spree::CorporateAccount < Spree::Base
  has_many :account_prices, as: :accountable, autosave: true
  accepts_nested_attributes_for :account_prices
end

所以在我的公司编辑视图中,我有这个。

<%= f.fields_for :account_prices do |ff| %> 
      <div class="row">
         <%= ff.fields_for :variant_price_set do |fff|%>
           <div class="col-md-2" >
             <%= fff.text_field :id %>
           </div>
           <div class="col-md-2">
              <%= fff.text_field :name %>
           </div>
         <% end %>

         <%= ff.fields_for :shipping_method_price_set do |fff|%>
           <div class="col-md-2">
              <%= fff.text_field :id %>
           </div>
           <div class="col-md-2">
             <%= fff.text_field :name %>
           </div>
         <% end %>

         <div class="col-md-2">
          start_date: <%= ff.text_field :exp_start_date%>
        </div>
        <div class="col-md">
           end_date: <%= ff.text_field :exp_end_date%>
        </div>

        <div class="col-md">
          staus: <%= ff.text_field :status%>
       </div>
    </div>
<% end %>

在我的公司财务总监那里,我有这个。

module Spree
  module Admin
    class CorporateAccountsController < ResourceController
      def update

        if @corporate_account.update_attributes(corporate_account_params)
          flash.now[:success] = Spree.t(:corporate_account_updated)
        end

        render :edit
      end

      def corporate_account_params
        params.require(:corporate_account).permit(:company_id,
                                                  :company_name,
                                                  :shipping_category_id,
                                                  :variant_price_set_id,
                                                  :shipping_method_price_set_id,
                                                  :corporate_token,
                                                  :corporate_token_confirmation,
                                                  :account_key,
                                                  :managed,
                                                  :billing_type,
                                                  :tax_exempt,
                                                  ship_address_attributes: permitted_address_attributes,
                                                  bill_address_attributes: permitted_address_attributes,
                                                  account_prices_attributes: [
                                                    :id, :exp_start_date, :exp_end_date, :status,
                                                    shipping_method_price_set_attributes: [:id, :name], 
                                                    variant_price_set_attributes: [:id, :name]
                                                  ]
                                                 )
      end
    end
  end
end

强大参数上的所有内容都会更新。除了那些深深的嵌套。 shipping_method_price_set_attributesvariant_price_set_attributes

因此,当我更改任何价格集上的ID并使用vscode断点将其捕获到公司控制器上时,params值都不会改变。就像当我输入3并击中断点时一样,检查参数是否仍然是先前的数据。任何想法?我认为这是一个错误。因为在此之前,我有variant_price_set的更新很好,但是当我添加shipping_method_price_set时,它不再更新了。希望我的问题有意义

示例参数

{
  "utf8": "✓",
  "authenticity_token": "IOZB+SbHtP+rrK2j1LqI039lOe8BB95vQXChUERb8dWNcM+YqUSaur3wMxWHmxDWFyIq8SkvQ9E7aJmv7TV5iw==",
  "corporate_account": {
    "company_id": "waaaaaaaaaa",
    "company_name": "QQQ",
    "corporate_token": "",
    "corporate_token_confirmation": "",
    "account_key": "",
    "tax_exempt": "0",
    "billing_type": "creditcard",
    "shipping_category_id": "",
    "account_prices_attributes": {
      "0": {
        "variant_price_set_attributes": {
          "id": "2",
          "name": "Another Test Price Set"
        },
        "shipping_method_price_set_attributes": {
          "id": "2",
          "name": "Test Shipping Method Price Set 2"
        },
        "exp_start_date": "2019-10-11 17:42:36 UTC",
        "exp_end_date": "2019-11-10 17:42:36 UTC",
        "status": "temporary",
        "id": "2"
      },
      "1": {
        "variant_price_set_attributes": {
          "id": "1",
          "name": "Test Price Set"
        },
        "shipping_method_price_set_attributes": {
          "id": "1",
          "name": "Test Shipping Method Price Set"
        },
        "exp_start_date": "2019-10-11 17:42:36 UTC",
        "exp_end_date": "2019-10-11 17:42:36 UTC",
        "status": "temporary",
        "id": "1"
      }
    }
  },
  "button": "",
  "id": "1"
}

更新网络选项卡后的请求

corporate_account[company_id]: waaaaaaaaaa
corporate_account[company_name]: QQQ
corporate_account[corporate_token]: 
corporate_account[corporate_token_confirmation]: 
corporate_account[account_key]: 
corporate_account[tax_exempt]: 0
corporate_account[billing_type]: creditcard
corporate_account[shipping_category_id]: 
corporate_account[account_prices_attributes][0][variant_price_set_attributes][id]: 3
corporate_account[account_prices_attributes][0][variant_price_set_attributes][id]: 2
corporate_account[account_prices_attributes][0][shipping_method_price_set_attributes][id]: 2
corporate_account[account_prices_attributes][0][shipping_method_price_set_attributes][id]: 2
corporate_account[account_prices_attributes][0][exp_start_date]: 
corporate_account[account_prices_attributes][0][exp_end_date]: 
corporate_account[account_prices_attributes][0][status]: default
corporate_account[account_prices_attributes][0][id]: 2
corporate_account[account_prices_attributes][1][variant_price_set_attributes][id]: 3
corporate_account[account_prices_attributes][1][variant_price_set_attributes][id]: 1
corporate_account[account_prices_attributes][1][shipping_method_price_set_attributes][id]: 2
corporate_account[account_prices_attributes][1][shipping_method_price_set_attributes][id]: 1
corporate_account[account_prices_attributes][1][exp_start_date]: 
corporate_account[account_prices_attributes][1][exp_end_date]: 
corporate_account[account_prices_attributes][1][status]: default
corporate_account[account_prices_attributes][1][id]: 1

更新2

因此,我使用具有相同params结构的失眠症进行了请求。但是当我尝试更改这些值

"variant_price_set_attributes": {
          "id": "1"
        },
        "shipping_method_price_set_attributes": {
          "id": "1"
        },

我遇到了路由错误。这很奇怪。

UPDATE 2.1 所以我发现了另一件事。出于某种原因,尝试调试时出现失眠。当我更改variant_priceshipping_method的值时,我会得到此堆栈错误

Started PUT "/spree/admin/corporate_accounts" for ::1 at 2019-10-14 12:06:55 -0700
User excluded error: #<ActionController::RoutingError: No route matches [PUT] "/spree/admin/corporate_accounts">

ActionController::RoutingError (No route matches [PUT] "/spree/admin/corporate_accounts"):
  actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'

如果您检查“用户排除”错误,则说没有路由与其匹配,这是因为出于某种奇怪的原因而丢失了Corporate_accounts的ID号。路线应该/spree/admin/corporate_accounts/1,这真棒,令人讨厌。

0 个答案:

没有答案