12

I am trying to save a array using the strong_parameters gem. But I am having issues with how the form is sending my array. The params look like this:

> params[:circuit] 
=> {"title"=>"Some title", ..., "viewable_tasks"=>{"0"=>"woop", "1"=>"dee", ...}}

And my circuit_params function looks like:

def circuit_params
  params.require(:circuit).permit(:title, :id, viewable_tasks: { }, ... )
end

I can't seem to get the syntax to allow my params to work. What I get in my console is:

> circuit_params
=> {"title"=>"implement plug-and-play mindshare",
 "viewable_tasks"=>{"0"=>nil, "1"=>nil, "2"=>nil}, ...

In my model I have:

class Circuit < ActiveRecord::Base
   serialize :viewable_tasks, Array
   ...
end

I noticed that I can get it to work properly with attributes I call accepts_nested_attributes_for on, so this may have something to do with it.

Thanks for any help

3 Answers 3

16

I just had the same issue and this was the correct syntax:

def circuit_params
  params.require(:circuit).permit(:title, :id, {:viewable_tasks => []}, ... )
end
Sign up to request clarification or add additional context in comments.

Comments

0

the serialized items must be in the end of the permit parameters. In my experience (and i dont know exactly why), you cannot input another unserialized item after a serialized one... try and let us know.

2 Comments

It's because of Ruby syntax. It's not a Rails restriction. Ruby allows a hash to be at the end of a parameter list without enclosing it in {braces}. If a hash is not at the end, it must be in braces.
I'm not sure why, @Hamdan. The ability to leave the braces off a hash at the end is probably for convenience and readability and to encourage a convention of a hash being the last param. In the Rails permit method, BTW, you totally can put a hash in the beginning or middle of the list if you use the braces.
-1

Try using this:

def circuit_params
  params.require(:circuit).permit(:title, :id, viewable_tasks:[], ... )
end

1 Comment

As I said, the hashes need to necessarily be at the end of the permit parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.