在 rails4 中设置 hstore,动态键/值 [英] setting hstore in rails4, dynamic key/values

查看:20
本文介绍了在 rails4 中设置 hstore,动态键/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次在 rails4 应用程序中使用 Hstore,我在表单中使用 javascript 为 hstore 列构建动态表单字段 (:schema)

I'm playing around with Hstore for the first time in a rails4 app, and I am using javascript in a form to build out dynamic form fields for the hstore column (:schema)

在 rails 4 中,我不需要在我的模型中添加任何 setter/getter 方法,对吗?

In rails 4 I dont need to add any setter/getter method in my model, correct?

在我的表单中,我正在构建动态输入字段并允许用户设置键/值对.很像 Hstore Heroku 演示应用

In my form i am building dynamic input fields and allowing the user to set the key/value pairs. Much like the Hstore Heroku Demo App

所以基本上我的表单会有像

So basically my form would have inputs like

input name="app[schema][dynamic_key1]" value="whatever value"
input name="app[schema][dynamic_key2]" value="whatever value2"

在我的应用控制器中:

def app_params
  params.require(:app).permit(:name, :title, :schema )
end

但是,当我创建一个新的应用程序记录时,我的架构 hstore 值没有保存.我看到了一些关于为 :schema => [] 制作强参数的事情,但这仍然不起作用.

However, when i create a new App record, my schema hstore values are not saving. I saw some things about making the strong param for :schema => [] but that still does not work.

因为我不知道这些值是什么,所以我不能像我在很多例子中看到的那样为这些设置 store_accessors.

Since I do not know what these values will be, i cant setup store_accessors for these like I have seen in a lot of examples.

推荐答案

在这里找到:http://guides.rubyonrails.org/action_controller_overview.html#more-examples

在我使用的控制器中:

def app_params
  params.require(:app).permit(:name, :title).tap do |whitelisted|
    whitelisted[:schema] = params[:app][:schema]
  end
end

这篇关于在 rails4 中设置 hstore,动态键/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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