更改作为参数接收的嵌套键的名称

时间:2018-12-11 10:24:15

标签: ruby-on-rails strong-parameters

嗨,在嵌套参数中传递了嵌套参数,并将属性附加到键的后面,然后传递给允许

如果我收到正常的哈希值,并且想在调用允许之前将属性附加到每个嵌套键中

该怎么做?

var t T 
i = &t

} }

这是我收到的请求,我想将属性附加到project_roles,project_role_skill等,以便Rails接受

请任何人帮助

2 个答案:

答案 0 :(得分:0)

您尝试过吗? Rails 4 - Strong Parameters - Nested Objects 如果您不满意,可以使用permit指定当前的表单和当前的方法吗?

答案 1 :(得分:0)

TL; DR:

include_once('DB_Handler.php');

$result = mysqli_query($mysqli, "SELECT DocentID FROM gebruiker");

$rows = array();
while ( $row = mysqli_fetch_array($result))
{
    $rows[] = $row;
}

echo json_encode($rows);

用法示例:

def params_with_deep_appended_nested_attributes(_params)
  modified_params = _params.clone

  _params.each do |k, v|
    if v.is_a?(Array)
      modified_params["#{k}_attributes"] = []

      v.each_with_index do |vv, index|
        if vv.is_a?(ActionController::Parameters)
          modified_params["#{k}_attributes"][index] = params_with_deep_appended_nested_attributes(vv)
        end
      end

      modified_params.delete(k)

    elsif v.is_a?(ActionController::Parameters)
      modified_params["#{k}_attributes"] = params_with_deep_appended_nested_attributes(v)
      modified_params.delete(k)
    end
  end

  modified_params
end

解决方案:

# rails console
example_json_hash_request = {
  "project": {
    "project_name":"test",
    "tentative_start_date":"2018-12-12",
    "tentative_end_date":"2019-12-12",
    "project_roles": [
       {  
          "role_id":1,
          "project_role_skills":[  
             {  
                "skill":{  
                   "skill_type":"C++",
                   "id":2
                }
             }
          ],
          "project_role_users":[  

          ],
          "role_end_date":"2018-12-12",
          "role_start_date":"2018-12-12"
       }
    ]
  }
}

# simulate `params` value in the controller
params = ActionController::Parameters.new(example_json_hash_request)

modified_params = params_with_deep_appended_nested_attributes(params)

pp modified_params.permit!.to_hash
# {"project_attributes"=>
#   {"project_name"=>"test",
#    "tentative_start_date"=>"2018-12-12",
#    "tentative_end_date"=>"2019-12-12",
#    "project_roles_attributes"=>
#     [{"role_id"=>1,
#       "role_end_date"=>"2018-12-12",
#       "role_start_date"=>"2018-12-12",
#       "project_role_skills_attributes"=>
#        [{"skill_attributes"=>{"skill_type"=>"C++", "id"=>2}}],
#       "project_role_users_attributes"=>[]}]}}

不要忘记在模型中定义“嵌套属性”:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  # ...
  # I added `= params` to default to the `params` value here in the controller
  def params_with_deep_appended_nested_attributes(_params = params)
    modified_params = _params.clone

    _params.each do |k, v|
      if v.is_a?(Array)
        modified_params["#{k}_attributes"] = []

        v.each_with_index do |vv, index|
          if vv.is_a?(ActionController::Parameters)
            modified_params["#{k}_attributes"][index] = params_with_deep_appended_nested_attributes(vv)
          end
        end

        modified_params.delete(k)

      elsif v.is_a?(ActionController::Parameters)
        modified_params["#{k}_attributes"] = params_with_deep_appended_nested_attributes(v)
        modified_params.delete(k)
      end
    end

    modified_params
  end
  # ...
end

# app/controllers/projects_controller.rb
class ProjectsController < ApplicationController
  def create
    @project = Project.new(project_params)

    if @project.save
      # ...
    else
      # ...
    end
  end

  def project_params
    params_with_deep_appended_nested_attributes.require(:project_attributes).permit(
      :project_name, :tentative_start_date, :tentative_end_date,
      project_roles_attributes: [
        :role_id, :role_end_date, :role_start_date,
        project_role_skills_attributes: [
          skill_attributes: [
            :skill_type, :id
          ]
        ],
        project_role_users_attributes: []
      ]
    )
  end
end

待办事项:

  • 添加# app/models/project.rb class Project < ApplicationRecord has_many :project_roles accepts_nested_attributes_for :project_roles end # app/models/project_role.rb class ProjectRole < ApplicationRecord belongs_to :project has_many :project_role_skills has_many :project_role_users accepts_nested_attributes_for :project_role_skills accepts_nested_attributes_for :project_role_users end # app/models/project_role_skill.rb class ProjectRoleSkill < ApplicationRecord belongs_to :project_role belongs_to :skill accepts_nested_attributes_for :skill end 返回值的缓存,以避免每次调用params_with_deep_appended_nested_attributes时都运行代码。

这个问题使我感兴趣。将来我可能会发现我的这种代码有用。