ruby-on-rails - 强参数 - 无法访问深层嵌套的属性

标签 ruby-on-rails ruby strong-parameters

一个 StackOverflow 问题问我需要什么,但 self 回答并没有帮助我知道下一步该做什么。该问题 ( whitelisting deeply nested strong parameters in rails ) 中提出的场景与我正在做的差不多,但我将发布我的缩写(仍然很长)并希望有人——甚至可能是帖子的戴夫——可以提供帮助(我没有足够的声誉来评论和问他)。关于嵌套强参数的链接很少,我还没有读过,我已经处理了很多 Controller 和 API 端点上的一些链接,但这是应用程序中最复杂的。 (我包含了一个很长的例子,所以你可以看到完整的复杂性。)

这是在 sales_controller 上,我们无法获取的属性之一是 timezone_name,它在 run_spans_attributes 中,它位于 sale 下的 options_attributes 中。我已经在 StackOverflow 上尝试了几乎所有与大多数嵌套属性匹配的不同语法方法,这些方法存在强参数问题,但没有一个有效。我需要更多类(class)吗?有魔术括号吗?我需要新的建议。请。

应该注意的是,这是针对 strong_parameters gem 和 Rails 3.2.21 的,但我想让应用程序为 Rails 4 做好准备,所以我希望避免短期解决方案。

抱歉太长了:

 Parameters:
    "sale"=>{
      "cloned_from"=>"",
      "type"=>"Localsale",
      "primary_contact_attributes"=>{
          "primary"=>"true",
          "first_name"=>"Fred",
          "id"=>"1712"
        },
      "contract_signed_on"=>"March 20, 2015",
      "billing_addresses_attributes"=>{
          "0"=>{
              "billing"=>"1",
              "city"=>"San Diego",
              "id"=>"29076"
            }
        },
      "other_contacts_attributes"=>{
          "0"=>{
              "first_name"=>"Fred",
              "_destroy"=>"false",
              "id"=>"170914"
            },
          "1"=>{
              "first_name"=>"Fred",
              "last_name"=>"Smith",
              "_destroy"=>"false",
              "id"=>"1798"
            }
        },
      "opportunity_detail_attributes"=>{
          "original_salesperson_id"=>"",
          "id"=>"10130"
        },
      "production_editor"=>"1868097",
      "event_sale_attributes"=>{
          "0"=>{
              "name"=>"is_super_sale",
              "value"=>"0",
              "id"=>"15326"
            },
          "1"=>{
              "name"=>"super_show_code",
              "value"=>""
            },
        },
      "scheduling_note"=>"",
      "category_ids"=>["2", "364"],
      "options_attributes"=>{
          "0"=>{
              "title"=>"T-Shirt and Bag Check",
              "event_starts_at(1i)"=>"2015",
              "event_starts_at(2i)"=>"6",
              "event_doors_open_at_attributes"=>{
                  "option_id"=>"8682604",
                  "doors_time(1i)"=>"",
                  "id"=>"278382"
                },
              "event_option_attributes"=>{
                  "0"=>{
                      "name"=>"event_duration",
                      "value"=>""
                    },
                  "1"=>{
                      "name"=>"send_pre_event_email",
                      "value"=>"1",
                      "id"=>"632546"
                    }
                },
              "language_id"=>"1",
              "run_spans_attributes"=>{
                  "0"=>{
                      "timezone_name"=>"Eastern Time (US & Canada)",
                      "_destroy"=>"false",
                      "id"=>"560878"
                    },
                  "1429320288130"=>{
                      "timezone_name"=>"Eastern Time (US & Canada)",
                      "_destroy"=>"false"
                    }
                },
              "_destroy"=>"false",
              "id"=>"8682604"
              }#ends 0 option
          },#ends options
      "coupons_per_redemption"=>"1",
      "methods_attributes"=>{
          "0"=>{
              "redemption_code"=>"0",
              "_destroy"=>"0",
              "id"=>"9797012"
            },
          "1"=>{
              "redemption_code"=>"4",
              "_destroy"=>"1",
              "vendor_provided_promo_code"=>"0",
              "promo_code"=>""
            }
        }, #ends redemption methods
      "addresses_attributes"=>{
          "0"=>{
              "street_address_1"=>"2400 Cat St",
              "primary"=>"0",
              "id"=>"2931074",
              "_destroy"=>"false"
            }
        },
      "zoom"=>"",
      "video_attributes"=>{
          "youtube_id"=>"",
        },
      "updated_from"=>"edit"
      }

帮我做这个好吗?顺便说一下,各种.tap do |whitelisted| 方法都失败了。

 private
 def_sale_strong_params
    params.require(:sale).permit(:how, :the, :heck, :do, :the_attributes =>
       [:make, themselves => [:known, :outside => [:of, :these => [:darn,
        :parentheses], :and], :brackets]])
 end

最佳答案

<强>1。在控制台中验证您的进度

要配置强参数,它可以帮助在 Rails 控制台中工作。您可以将参数设置到 ActiveSupport::HashWithIndifferentAccess 对象中,然后开始测试从 ActionController::Parameters 返回的内容。

例如,假设我们从:

params = ActiveSupport::HashWithIndifferentAccess.new(
  "sale"=>{
    "cloned_from"=>"",
    "type"=>"Localsale"}
)

我们可以运行这个:

ActionController::Parameters.new(params).require(:sale).permit(:cloned_from, :type)

我们会得到这个作为返回值,我们会知道我们已经成功地允许 cloned_fromtype:

{"cloned_from"=>"", "type"=>"Localsale"}

您可以继续工作,直到考虑到所有参数。如果您在问题中包括了整套参数...

params = ActiveSupport::HashWithIndifferentAccess.new(
  "sale"=>{
    "cloned_from"=>"",
    "type"=>"Localsale",
    ...
    "options_attributes"=>{
      "0"=>{
        "title"=>"T-Shirt and Bag Check",
        ...
        "run_spans_attributes"=>{
          "0"=>{
            "timezone_name"=>"Eastern Time (US & Canada)",
            ...
)

...您可以使用如下所示的结构深入到 timezone_name:

ActionController::Parameters.new(params).require(:sale).permit(:cloned_from, :type, options_attributes: [:title, run_spans_attributes: [:timezone_name]])

控制台中的返回值为:

{"cloned_from"=>"", "type"=>"Localsale", "options_attributes"=>{"0"=>{"title"=>"T-Shirt and Bag Check", "run_spans_attributes"=>{"0"=>{"timezone_name"=>"Eastern Time (US & Canada)"}, "1429320288130"=>{"timezone_name"=>"Eastern Time (US & Canada)"}}}}}

<强>2。将工作分解成更小的部分

尝试在一行中处理每个模型的所有允许属性可能会让人感到困惑。您可以将其分成几行,以使事情更容易理解。从这个结构开始,为每个模型填写额外的属性:

video_attrs = []  # Fill in these empty arrays with the allowed parameters for each model
addresses_attrs = []
methods_attrs = []
run_spans_attrs = [:timezone_name]
event_option_attrs = []
options_attrs = [:title, event_option_attributes: event_option_attrs, run_spans_attributes: run_spans_attrs]
event_sale_attrs = []
opportunity_detail_attrs = []
other_contacts_attrs = []
billing_addresses_attrs = []
primary_contact_attrs = []
sales_attributes = [
  :cloned_from, 
  :type, 
  primary_contact_attributes: primary_contact_attrs, 
  billing_addresses_attributes: billing_addresses_attrs,
  other_contacts_attributes: other_contacts_attrs,
  options_attributes: options_attrs, 
  opportunity_detail_attributes: opportunity_detail_attrs,
  event_sale_attributes: event_sale_attrs,
  methods_attributes: methods_attrs,
  addresses_attributes: addresses_attrs,
  video_attributes: video_attrs
]

然后您可以将 *sales_attributes 发送到允许的参数中。您可以在控制台中验证:

ActionController::Parameters.new(params).require(:sale).permit(*sales_attributes)

关于ruby-on-rails - 强参数 - 无法访问深层嵌套的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29714451/

相关文章:

ruby-on-rails - 将 Facebook、twitter、google plus 集成到另一个应用程序中

ruby-on-rails - 我无法在 OSX 10.7.5 上安装 Rails

ruby-on-rails - 使用 Paperclip、ImageMagick、Jcrop 和 S3 存储进行裁剪 : Why won't 'image.reprocess!' reprocess?

windows - 如何仅通过名称从命令行启动 ruby​​ 脚本?

ruby - RVM 安装 1.9.2 头错误

ruby-on-rails - Rails 4 中的strong_parameters gem 和自定义设计路线

ruby-on-rails-4 - Rails 4 多嵌套表单和强参数

ruby-on-rails - 从 Rails 中的注册表单创 build 计用户时如何创建另一个对象?

ruby-on-rails - bundle 有什么意义?

ruby-on-rails - 不同模型的 Rails 新 Action