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

查看:60
本文介绍了在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

found this here: http://guides.rubyonrails.org/action_controller_overview.html#more-examples

在我的控制器中,我使用了:

and in my controller I used:

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

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

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