Strong Parameters Not Accepting Array

strong parameters not accepting array

Answering myself, I got it working not directly, but the below method from the Strong Parameters issues discussion helped me in converting a normal parameter to a whitelisted one.

def user_params
params.require(:user).permit(:first_name).tap do |whitelisted|
whitelisted[:role_ids] = params[:user][:role_ids]
end
end

rails strong parameter not accepting array of hashes

Solved the problem by adding the group_members field inside permit args.

def group_params
params.require(:group).permit(:group_name, :group_title, group_members: [:id, :darknet_accountname, :access_level])
end

After this there was no complaint about unpermitted parameters within group_members.

Rails Strong Parameters - Allow parameter to be an Array or String

You could check if params[:strings] is an array and work from there

def strong_params
if params[:string].is_a? Array
params.fetch(:search, {}).permit(strings: [])
else
params.fetch(:search, {}).permit(:strings)
end
end

how to permit an array with strong parameters

This https://github.com/rails/strong_parameters seems like the relevant section of the docs:

The permitted scalar types are String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO,
ActionDispatch::Http::UploadedFile and Rack::Test::UploadedFile.

To declare that the value in params must be an array of permitted scalar values map the key to an empty array:

params.permit(:id => [])

In my app, the category_ids are passed to the create action in an array

"category_ids"=>["", "2"],

Therefore, when declaring strong parameters, I explicitly set category_ids to be an array

params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids => [])

Works perfectly now!

(IMPORTANT: As @Lenart notes in the comments, the array declarations must be at the end of the attributes list, otherwise you'll get a syntax error.)

Strong parameters in rails with array as root object

Ideally you should always send a JSON objects as a param, when you don't explicitly give a key for object, it takes _json as is the case with your params.

Anyway to write a strong parameter in your case, you can do something like,

params = ActionController::Parameters.new({
"_json" => [
{"id"=>1, "fruit_ids"=>[1, 2, 3]},
{"id"=>2, "fruit_ids"=>[4, 5, 6]}
],
"format"=>"json",
"controller"=>"...",
"action"=>"..."
}

params.permit('_json': [:id, fruit_ids: []]) # You need this line.

# => {"_json"=>[{"id"=>1, "fruit_ids"=>[1, 2, 3]}, {"id"=>2, "fruit_ids"=>[4, 5, 6]}]}

Rails: Accept 2D array of strings with strong parameters

The best way I've found to get this done is to convert the 2D array to a 1D array, permit the 1D array, and then convert the array back to 2D.

nested_facet_filters = []
(params[:requests] || []).each_with_index do |r, ridx|
if r[:params].key?(:facetFilters) && r[:params][:facetFilters].kind_of?(Array) && r[:params][:facetFilters].first.kind_of?(Array)
# flatten the facet filters into a 1D array because Rails can't permit a 2D array
nested_facet_filters << ridx
r[:params][:facetFilters] = r[:params][:facetFilters].flatten
end
end

permitted = params.permit({
requests: [
:query,
:params => [
:facets => [],
:facetFilters => [],
]
]
}).to_h

# after permitting the params, we need to convert the facetFilters back into a 2D array
if nested_facet_filters.present?
nested_facet_filters.each do |idx|
# create map from facet key to array of values
d = {}
permitted[:requests][idx][:params][:facetFilters].each do |s|
split = s.split(':')
facet = split.first
value = split[1..-1].join(':')
if d.key?(facet)
d[facet] << s
else
d[facet] = [s]
end
end
# mutate facetFilters back to 2D array for posting to Algolia
permitted[:requests][idx][:params][:facetFilters] = d.values
end
end

There's been a merge request to accept 2D arrays for six years open in the Rails issue tracker [thread]...



Related Topics



Leave a reply



Submit