Skip to content

Commit

Permalink
Merge pull request rails#49749 from fatkodima/fix-extract_value
Browse files Browse the repository at this point in the history
Fix `StrongParameters#extract_value` to include blank values
  • Loading branch information
byroot authored Oct 23, 2023
2 parents 55bf2df + 97ae6e7 commit 7de3400
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 7 additions & 1 deletion actionpack/lib/action_controller/metal/strong_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,14 @@ def deep_dup
# params.extract_value(:id) # => ["1", "123"]
# params.extract_value(:tags, delimiter: ",") # => ["ruby", "rails"]
# params.extract_value(:non_existent_key) # => nil
#
# Note that if the given +key+'s value contains blank elements, then
# the returned array will include empty strings.
#
# params = ActionController::Parameters.new(tags: "ruby,rails,,web")
# params.extract_value(:tags) # => ["ruby", "rails", "", "web"]
def extract_value(key, delimiter: "_")
@parameters[key]&.split(delimiter)
@parameters[key]&.split(delimiter, -1)
end

protected
Expand Down
4 changes: 3 additions & 1 deletion actionpack/test/controller/parameters/accessors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,13 @@ class ParametersAccessorsTest < ActiveSupport::TestCase
test "#extract_value splits param by delimiter" do
params = ActionController::Parameters.new(
id: "1_123",
tags: "ruby,rails,web"
tags: "ruby,rails,web",
blank_tags: ",ruby,,rails,"
)

assert_equal(["1", "123"], params.extract_value(:id))
assert_equal(["ruby", "rails", "web"], params.extract_value(:tags, delimiter: ","))
assert_equal(["", "ruby", "", "rails", ""], params.extract_value(:blank_tags, delimiter: ","))
assert_nil(params.extract_value(:non_existent_key))
end
end

0 comments on commit 7de3400

Please sign in to comment.