本文介绍了使用 strong_parameters 序列化数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 strong_parameters gem 保存数组.但是我对表单如何发送我的数组有问题.参数如下所示:

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", ...}}

我的 circuit_params 函数看起来像:

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}, ...

在我的模型中,我有:

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

我注意到我可以使用我调用 accepts_nested_attributes_for 的属性让它正常工作,所以这可能与它有关.

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.

感谢您的帮助

推荐答案

我刚刚遇到了同样的问题,这是正确的语法:

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

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

这篇关于使用 strong_parameters 序列化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 05:41