Rails:忽略具有特定名称的嵌套属性?

时间:2011-03-08 21:22:35

标签: ruby-on-rails ruby-on-rails-3 nested-attributes

基于Ryan Bates Nested Model Form教程的这个功能,当我提交表单时,我已经提交了这组数据:

{
    "authenticity_token"=>"mdohADjieoqUwOwUK/H52TAiH5HdFYQtTbTuMJtDJE8=",
     "utf8"=>"✓",
     "id"=>"92",
     "survey"=>
    {
        "questions_attributes"=>
        {
            "1299616889104"=>
            {
                "display_type"=>"",
                 "text"=>"",
                 "other"=>"0",
                 "display_order"=>"0",
                 "answer_choices"=>"",
                 "item_type"=>"multiple_choice"
            },
             "new_questions"=>
            {
                "display_type"=>"compare",
                 "pick"=>"one",
                 "text"=>"",
                 "other"=>"0",
                 "display_order"=>"0",
                 "answer_choices"=>"",
                 "item_type"=>"compare"
            }
        },
         "id"=>"92"
    }
}

我需要弄清楚如何做的是忽略new_questions部分。我知道您可以将reject_if添加到accepts_nested_attributes_for,但是隐藏的字段已包含并预先填充了数据,因此我无法根据空字段进行忽略。

那么如何在我的示例数据中忽略new_questions

我正在运行Rails 3.0.3

1 个答案:

答案 0 :(得分:0)

我会研究使用条件验证。下面是railscast处理密码的问题,但您可以根据自己的情况轻松调整类似内容