rails 4 强参数 + 动态 hstore 键 [英] rails 4 strong params + dynamic hstore keys
问题描述
我在使用 Hstore 和动态访问器克服 Rails 4 中新的强参数要求时遇到问题
I'm having a problem overcoming the new strong params requirement in Rails 4 using Hstore and dynamic accessors
我有一个名为 :content
的 Hstore 列,我想用它来存储多种语言的内容,即 :en、:fr
等.但我没有不知道在模型或控制器中预先设置它们的语言.
I have an Hstore column called :content
which I want to use to store content in multiple languages, ie :en, :fr
, etc. And I don't know which language upfront to set them in either the model or the controller.
store_accessor :content, [:en, :fr] #+226 random other il8n languages won't work.
如何在 rails 4 中为一列覆盖强参数(或允许动态 hstore 键)?
How can I override strong params (or allow for dynamic hstore keys) in rails 4 for one column?
params.require(:article).permit(
:name, :content,
:en, :fr #+226 random translations
)
缺少...
params.require(:article).permit!
当然可以.
推荐答案
如果我理解正确,您希望将动态键的哈希列入白名单.您可以使用一些 ruby
代码如下:
If I understand correctly, you would like to whitelist a hash of dynamic keys. You can use some ruby
code as follows to do this:
params.require(:article).permit(:name).tap do |whitelisted|
whitelisted[:content] = params[:article][:content]
end
这对我有用,希望有帮助!
This worked for me, hope it helps!
这篇关于rails 4 强参数 + 动态 hstore 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!