序列化哈希字段和简单表单 [英] Serialized Hash field and Simple Form

查看:39
本文介绍了序列化哈希字段和简单表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容.

class Page < ActiveRecord::Base
  belongs_to :category
  serialize :fields
end

fields 的值将取决于类别.但作为一个例子;

The value of fields will depend on the category. But as an example;

{"address" => "8 finance street, hong kong",
 "founded" => "1973"}

在此示例中,类别已将 "address""founded" 定义为自定义字段.

In this example the category has defined "address" and "founded" as the custom fields.

我想说的是;

= simple_form_for(@page) do |f|
  = f.association :category
  - f.object.category.fields.each do |field|
    = f.input field.name

但我需要做一些魔法来处理 @page.founded 无效

But I need to do something magic to deal with the fact that @page.founded is not valid

相反,我应该查看 @page.fields["founded"].

有什么建议吗?

更新:

我离得更近了

- if f.object.category
  - f.object.category.fields.each do |field|
    = f.input field.name do
      = text_field_tag "post[fields][#{field.name}]", f.object.fields[file.name]

现在需要制作这个DRYer(不想指定对象的名字).

Now need to make this DRYer (don't want to specify the name of the object).

我会看看我是否可以为此编写一个像样的简单表单扩展.

I'll see if I can write a decent simple form extension for this.

推荐答案

我在尝试对 Mongoid 模型的 Hash 字段类型使用 simple_fields_for 时遇到了类似的问题.我正在处理的示例的 Mongoid 版本如下所示:

I ran into a similar issue trying to use simple_fields_for on a Hash field type of a Mongoid model. The Mongoid version of your example I was dealing with looked like this:

class Page
  include Mongoid::Document
  field :fields, type: Hash
end

不过,我的情况可能略有不同,因为我已经提前知道要查找的哈希键,并且只需要 simple_fields_for 来处理哈希字段.我使用的简单方法(基本的 fields_for 用法)如下所示:

My situation might be slightly different though, as I already know the hash keys I am looking for ahead of time, and just needed simple_fields_for to work with the hash field. The naive method (basic fields_for usage) I was using looked like this:

= simple_form_for(@page) do |f|
  = f.simple_fields_for :fields do |ff|
    = ff.input :address
    = ff.input :founded

但这并没有正确填充表单.nicholaides 将散列包装在结构中的解决方案对我有用:

But that wasn't populating the form properly. nicholaides's solution of wrapping the hash in a struct worked for me:

- require 'ostruct'
= simple_form_for(@page) do |f|
  = f.simple_fields_for :fields, OpenStruct.new(@page.fields) do |ff|
    = ff.input :address
    = ff.input :founded

为了避免将 OpenStruct 的内容混入我的视图中,我为 simple_form 创建了一个猴子补丁,以自动将哈希类型包装在 OpenStruct 中并将其放入初始化程序:

To avoid having to mix the OpenStruct stuff into my view, I created a monkey patch for simple_form to automatically wrap hash types in an OpenStruct and put it into an initializer:

require 'ostruct'

module SimpleForm::ActionViewExtensions::Builder
  def simple_fields_for_with_hash_support(*args, &block)
    if args[0] && !args[1]
      field = object.send(args[0])
      args << OpenStruct.new(field) if field.respond_to?(:has_key?)
    end
    simple_fields_for_without_hash_support(*args, &block)
  end
  alias simple_fields_for_without_hash_support simple_fields_for
  alias simple_fields_for simple_fields_for_with_hash_support
end

现在我可以在没有特殊视图代码的情况下使用我原来的天真解决方案.

And now I can use my original naive solution without special view code.

这篇关于序列化哈希字段和简单表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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