Rails 4 Undefined Array Parameters

I have an array field in my model and I'm trying to update it.

My strong parameters method below

def post_params params["post"]["categories"] = params["post"]["categories"].split(",") params.require(:post).permit(:name, :email, :categories) end 

My action in my controller is as follows

 def update post = Post.find(params[:id] if post and post.update_attributes(post_params) redirect_to root_url else redirect_to posts_url end end 

However, whenever I send an update message, in my development log I see

 Unpermitted parameters: categories 

Past parameters:

  Parameters: {"utf8"=>"βœ“", "authenticity_token"=>"auth token", "id"=>"10", "post"=>{"name"=>"Toni Mitchell", "email"=>"eileen_hansen@hayetokes.info", "categories"=>",2"}} 

I want to think that this has something to do with the fact that the categories attribute is an array, since everything else looks good. Again, I could be wrong. So what happened to my code and why I do not allow me to save the category field when it is explicitly allowed? Thank.

+49
ruby ruby-on-rails ruby-on-rails-4 strong-parameters
Jul 25 '13 at 21:06
source share
5 answers

try it

 params.require(:post).permit(:name, :email, :categories => []) 

(Do not pay attention to my comment, I do not think this is important)

+125
Jul 25 '13 at 21:45
source share
β€” -

in rails 4, what would be

 params.require(:post).permit(:name, :email, {:categories => []}) 
+37
03 Oct '13 at 4:24
source share

The allowed 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 the parameters should be an array of allowed scalar values, map the key to an empty array:

 params.permit(:id => []) 

Here is what the documentation of strong options on Github says:

 params.require(:post).permit(:name, :email, :categories => []) 

I hope this works for you.

+8
Jul 25 '13 at 21:53 on
source share

I had the same problem, but in my case I also had to change:

<input type="checkbox" name="photographer[attending]" value="Baku">

at

<input type="checkbox" name="photographer[attending][]" value="Baku">

Hope this helps someone.

+1
Apr 16 '15 at 18:24
source share

I had the same problem, but just adding an array for resolution was not enough. I also had to add a type. In this way:

 params.require(:transaction).permit(:name, :tag_ids => [:id]) 

I’m not sure if this is the ideal solution, but after that the log of Unpermitted parameters logs disappeared.

I found a hint for this solution from this excellent post: http://patshaughnessy.net/2014/6/16/a-rule-of-thumb-for-strong-parameters

+1
Jul 18 '15 at 8:36
source share



All Articles