Serialize array with strong_parameters
I just had the same issue and this was the correct syntax:
def circuit_params
params.require(:circuit).permit(:title, :id, {:viewable_tasks => []}, ... )
end
Rails access serialized array in form
With the introduction of strong parameters, arbitrary non-scalar values are no longer accepted as input values. If you want to pass an array of scalars, however, you can declare that in your permit
statement as in:
params.require(:question).permit(:category_id, :content, :choices => [], :answer_id)
This can be a difficult problem to detect, however, as the input values can simply be ignored without an error in some cases (the specifics of which I don't recall off-hand).
This is discussed further in how to permit an array with strong parameters
how to make rails strong_parameters + nested_attributes + serialize work?
Pfff - finally got it... With strong parameters no unknown keys can pass, so solution here was:
class EventsController < ApplicationController
...
def event_params
params.permit(client_attributes: [ {phones: [:number, :type]}])
end
end
Based on http://edgeapi.rubyonrails.org/classes/ActionController/Parameters.html#method-i-permit-21
Hope it helps someone.
I could specify keys in my serialisable field here, but what with user added keys? Is serialised field usable with strong parameters at all? (this probably should be a new question...)
How to permit the proper storage of serialized arrays?
This could lead to an issue if you're loading your data outside of rails, when ActionController::Parameters isn't defined.
Outside of that, this means you get an ActionController::Parameters object instead of a Hash.
Since they both behave the same way, you can just use them similarly.
For now, if you need a hash anyway, you should be able to do the following in your model:
before_save :serialize_to_hash
def serialize_to_hash
self.links = self.links.to_hash
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 and multidimensional arrays
You can also do this in this way
def permited_params
hash = params.permit(:attr1, :attr2)
hash[:attr3] = params.require(:attr3) if params.has_key?(:attr3)
hash
end
Permit array in params
params.permit(:name, :ingredients => [:name, :amount,:unit])
should do the trick.
Read Nested Parameters.
Related Topics
How to Require a Block in Ruby
How to Destroy a Record Without an Id Column in Ruby Activerecord
How to 'Unload' ('Un-Require') a Ruby Library
How to Fix a Slow Implicit Query on Pg_Attribute Table in Rails
How to Update Rails Locale Yaml File Without Loosing Comments and Variables
Why Do Ruby Procs/Blocks with Splat Arguments Behave Differently Than Methods and Lambdas
How to Split String into Array as Integers
Using Activerecord Find_In_Batches Method for Deleting Large Data
Is It Necessary to Close Stringio in Ruby
How to Use Regex for Utf8 in Ruby
Ssl_Connect Syscall Returned=5 Errno=0 State=Sslv2/V3 Read Server Hello A
Ruby: Yield Block from a Block
Ruby - Does Array a Contain All Elements of Array B
Call Before Methods in Model on Ruby
How to Sign_In for Devise to Test Controller with Minitest