本文介绍了您如何处理Active Admin资源中的序列化编辑字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,,它有一个文本字段,名称

 > rails g model域名:文本
调用active_record
创建db / migrate / 20111117233221_create_domains.rb
创建应用程序/模型/domain.rb
>耙db:migrate
== CreateDomains:迁移==================================== =============
-create_table(:domains)
-> 0.0015s
== CreateDomains:迁移(0.0066s)================================== ======

我将此字段设置为序列化为模型中的数组。

 #app / models / domain.rb 
class Domain< ActiveRecord :: Base
serialize:names,数组
end

创建ActiveAdmin此模型的资源

 > rails g active_admin:资源域
创建app / admin / domains.rb

然后,在app / admin / domains.rb,我设置了各种块来处理序列化字段,例如

 #app / admin / domains.rb 
ActiveAdmin.register域名

索引
id_column
列:names | domain |
#{domain.names.join(,,除非domain.names.nil?}
结束
default_actions
结束

显示做|域|
attributes_table
行:名称做
#{domain.names.join(,),除非domain.names.nil?}
结束
结束
结束

表格| f |
f.inputs域做
f.input:names
end
f.buttons
end

#取param [:domain] [:name]参数,
#拆分并保存到数组
before_save do | domain |
domain.names = params [:domain] [:names] .split(,)除非params [:domain] .nil?还是params [:domain] [:names] .nil?
结束
结束

几乎所有功能都很好-我的名字显示为逗号分隔在索引中并显示视图。当我将我的姓名字段设置为 a,b,c更新记录时,before_save会将其转换为一个数组,然后通过ActiveRecord序列化保存该数组。





特别是,如果我在domain.names字段中保存了以下数组:

 #域中保存的名称数组active_record 
domain.names = [ a, b, c ]

如何更改:

 格式| f | 
f.inputs域执行
f.input:名称
end
f.buttons
end

,以便在加载 edit 表单时,在文本字段中看到 abc ,您会看到 a,b,c

解决方案

以下是我如何处理这种情况的摘要。我在模型中添加了一个访问器,该访问器可以将Array变成由换行符连接的字符串,然后将其拆分回Array。

 #app / models / domain.rb 
class Domain< ActiveRecord :: Base
serialize:names,数组
attr_accessor:names_raw

def names_raw
self.names.join( \n),除非self.names 。零?
end

def names_raw =(values)
self.names = []
self.names = values.split( \n)
end
end

然后,在我的域管理资源中,而不是使用:names 字段,我使用了:names_raw 字段。设置该值将使用新值保存名称Array。

 #app / admin / domains.rb 
形式做| f |
f.inputs Domain执行
f.input:names_raw,:as => :text
结束
f.buttons
结束


I have a model, Domain, which has a text field, names.

> rails g model Domain names:text
  invoke  active_record
  create    db/migrate/20111117233221_create_domains.rb
  create    app/models/domain.rb
> rake db:migrate
==  CreateDomains: migrating ==================================================
-- create_table(:domains)
   -> 0.0015s
==  CreateDomains: migrated (0.0066s) =========================================

I set this field as serialized into an array in the model.

# app/models/domain.rb
class Domain < ActiveRecord::Base
  serialize :names, Array
end

Create the ActiveAdmin resource for this model

> rails g active_admin:resource Domain
    create  app/admin/domains.rb

then, in the app/admin/domains.rb, I setup the various blocks to handle the serialized field as such

# app/admin/domains.rb
ActiveAdmin.register Domain do

  index do
    id_column
    column :names do |domain|
      "#{domain.names.join( ", " ) unless domain.names.nil?}"
    end
    default_actions
  end

  show do |domain|
    attributes_table do
      row :names do
        "#{domain.names.join( ", " ) unless domain.names.nil?}"
      end
    end
  end

  form do |f|
    f.inputs "Domain" do
      f.input :names
    end
    f.buttons
  end

  # before we save, take the param[:domain][:name] parameter,
  # split and save it to our array
  before_save do |domain|
    domain.names = params[:domain][:names].split(",") unless params[:domain].nil? or params[:domain][:names].nil?
  end
end

Nearly everything works great -- my names are displayed as comma separated in the index and show views. When I update a record with my names field set to "a,b,c", the before_save works to turn that into an array that is then saved via the ActiveRecord serialize.

What I can not solve is how to make the edit form put in a comma-separated list into the text field. I tried using a partial and using formtastic syntax directly as well as trying to make it work via the active_admin DLS syntax. Does anyone know how to make this work?

Specifically, if I have the following array saved in my domain.names field:

# array of names saved in the domain active_record
domain.names = ["a", "b", "c"]

how to change:

      form do |f|
        f.inputs "Domain" do
          f.input :names
        end
        f.buttons
      end

so that when the edit form is loaded, in the text field instead of seeing abc, you see a,b,c.

解决方案

Here is a summary of how I handled this situation. I added an accessor to the model which can turn the Array into a string joined by a linefeed and split it back to an Array.

# app/models/domain.rb
class Domain < ActiveRecord::Base
  serialize       :names, Array
  attr_accessor   :names_raw

  def names_raw
    self.names.join("\n") unless self.names.nil?
  end

  def names_raw=(values)
    self.names = []
    self.names=values.split("\n")
  end
end

then, in my admin resource for domain, instead of using the :names field, I used the :names_raw field. setting this value would save the names Array with the new values.

# app/admin/domains.rb
form do |f|
  f.inputs "Domain" do
    f.input :names_raw, :as => :text
  end
  f.buttons
end

这篇关于您如何处理Active Admin资源中的序列化编辑字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 21:34