TL; DR:
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
TODO:
- добавьте кэширование возвращаемого значения
params_with_deep_appended_nested_attributes
, чтобы избежать запуска кода каждый раз, когда будет вызываться params_with_deep_appended_nested_attributes
.
Этот вопрос меня интересует.Я мог бы использовать этот код в будущем.