rails 4 强参数 + 动态 hstore 键 [英] rails 4 strong params + dynamic hstore keys

查看:37
本文介绍了rails 4 强参数 + 动态 hstore 键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆