【问题标题】:Cannot save a custom field (array) in ActiveAdmin and Mongoid 3.x无法在 ActiveAdmin 和 Mongoid 3.x 中保存自定义字段(数组)
【发布时间】:2012-08-11 20:30:59
【问题描述】:

我有一个字段“位置”,它是一个数组 [lng, lat]。

我在ActiveAdmin表单中有两个输入字段,定义如下

f.inputs :name => "Location" do
  f.input :latitude
  f.input :longitude
end

为了获得纬度和经度,我在我的模型中定义了两个吸气剂:

def latitude
  location[1]
end

def longitude
  location[0]
end

表单按预期显示。

为了保存这些值,我在模型中创建了两个 setter

def latitude=(lat)
  self[:location][1] = lat.to_f
end

def longitude=(lon)
  self[:location][0] = lon.to_f
end

在表单提交之后,这些方法被调用,但值不会被持久化。

我错过了什么吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongoid activeadmin mongoid3


    【解决方案1】:

    我找到了解决方案。当我使用 set() 时,它起作用了。 save 和 update_attribute - 没有。

    def latitude=(lat)
      self[:location][1] = lat.to_f
      self.set :location, self[:location]
    end
    
    def longitude=(lon)
      self[:location][0] = lon.to_f
      self.set :location, self[:location]
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 2020-12-14
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多