feat(controller): add read_param_value helper
This commit is contained in:
parent
cfeef3bf5f
commit
edba12705b
1 changed files with 16 additions and 0 deletions
|
@ -364,4 +364,20 @@ class ApplicationController < ActionController::Base
|
||||||
def set_customizable_view_path
|
def set_customizable_view_path
|
||||||
prepend_view_path "app/custom_views"
|
prepend_view_path "app/custom_views"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Extract a value from params based on the "path"
|
||||||
|
#
|
||||||
|
# params: { dossiers: { champs_attributes: { 1234 => { value: "hello" } } } }
|
||||||
|
#
|
||||||
|
# Usage: read_param_value("dossiers[champs_attributes][1234]", "value")
|
||||||
|
def read_param_value(path, name)
|
||||||
|
parts = path.split(/\[|\]\[|\]/) + [name]
|
||||||
|
parts.reduce(params) do |value, part|
|
||||||
|
if part.to_i != 0
|
||||||
|
value[part.to_i] || value[part]
|
||||||
|
else
|
||||||
|
value[part]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue