[英]How to permit the proper storage of serialized arrays?
I am using Rails 4.0.0 and I am trying to properly store a serialized array to the database permitting values with the StrongParameters gem. 我正在使用Rails 4.0.0,并且试图将一个序列化数组正确存储到数据库中,以允许使用StrongParameters gem值。 That is, I have:
也就是说,我有:
# Model class
class Article < ActiveRecord::Base
serialize :links, Array
...
end
# Controller class
class ArticlesController < ApplicationController
...
def update
...
@article.update_attributes(article_params)
...
end
private
def article_params
params.require(:article).permit(<PERMITTED_PARAMETERS>) # Keep reading for more information.
end
end
From the view submitted parameters are: 从视图中提交的参数为:
"links"=>{"0"=>{"name"=>"Web Site Name 0", "url"=>"www.website0.com"}, "1"=>{"name"=>"Web Site Name 1", "url"=>"www.website1.com"}, "2"=>{"name"=>"Web Site Name 2", "url"=>"www.website2.com"}, "3"=>{"name"=>"", "url"=>""}, "4"=>{"name"=>"", "url"=>""}, "5"=>{"name"=>"", "url"=>""}}}
In the above code, in place of <PERMITTED_PARAMETERS>
I tried to put in turn the following: 在上面的代码中,我尝试依次放置以下内容代替
<PERMITTED_PARAMETERS>
:
:links => [:name, :url]
[:links => [:name, :url]]
{:links => [:name, :url]}
[{:links => [:name, :url]}]
In all the above cases the StrongParameters correctly permits values without raising errors, however in my database it is always stored the following serialized data: 在上述所有情况下,StrongParameters正确地允许值而不会引发错误,但是在我的数据库中,它始终存储以下序列化数据:
---
- !ruby/hash:ActionController::Parameters
name: Web Site Name 0
url: www.website0.com
- !ruby/hash:ActionController::Parameters
name: Web Site Name 1
url: www.website1.com
- !ruby/hash:ActionController::Parameters
name: Web Site Name 3
url: www.website2.com
What !ruby/hash:ActionController::Parameters
means? !ruby/hash:ActionController::Parameters
是什么意思? Is it correct? 这是正确的吗? If no, how can I store data the proper way?
如果没有,我该如何正确存储数据? Is it the StrongParameters gem to cause that strange behavior?
是StrongParameters宝石导致这种奇怪的行为吗?
Note : All seems to work in my system even if data is "strange". 注意 :即使数据“奇怪”,所有内容似乎都可以在我的系统中工作。
I prefer to use a custom coder, take a look 我更喜欢使用自定义编码器,看看
# Model class
class Article < ActiveRecord::Base
serialize :links, ArticleCoder
...
end
and ... 还有...
class ArticleCoder
def self.load(data)
YAML.load(data) unless data.nil?
end
def self.dump(data)
YAML.dump(data.to_a) unless data.nil?
end
end
Let me know if this work for you. 让我知道这是否适合您。
This could lead to an issue if you're loading your data outside of rails, when ActionController::Parameters isn't defined. 如果未定义ActionController :: Parameters时将数据加载到rails之外,则可能会导致问题。 Outside of that, this means you get an ActionController::Parameters object instead of a Hash.
除此之外,这意味着您将获得一个ActionController :: Parameters对象而不是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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.