允许在嵌套属性中使用params数组[active admin]

时间:2017-11-27 09:14:15

标签: ruby-on-rails ruby activeadmin ruby-on-rails-4.2

如何在有效管理中的 permit_params 中指定 nested_attribute 的数组属性?

应用程序/模型/ event.rb

class Event < ActiveRecord::Base    
   has_many :other_details, dependent: :destroy    
   accepts_nested_attributes_for :other_details    
end

应用程序/模型/ other_detail.rb

class OtherDetail < ActiveRecord::Base    
    belongs_to :event    
end

应用程序/管理/ event.rb

ActiveAdmin.register Event do    
permit_params :other_details_attributes => [:id, :detail_type, :points]    
  form do |f|
    f.inputs do
    f.has_many :other_details do |ff, index|
        ff.input :detail_type
        ff.input :points, input_html: { value: (ff.object.points.join(", ").remove('%') if ff.object.points.present?)}
    end
  end
end

我正在

params['experience']['other_details_attributes'] = 
{"0"=>{"detail_type"=>"Itinerary","points"=>"point A, point B", "id"=>"37"},
"1"=>{"detail_type"=>"Schedule","points"=>"point C, point D", "id"=>"36"}}

如何获得这个?

params['experience']['other_details_attributes'] = 
{"0"=>{"detail_type"=>"Itinerary", "points"=>["point A", "point B"],"id"=>"37"},
"1"=>{"detail_type"=>"Schedule", "points"=>["point C", "point D"], "id"=>"36"}}

注意 -

  1. 我在字符串中积分 我需要数组

  2. 我尝试了 @ xeon131 建议的方法,但它导致了许多复杂的更新方法代码。

1 个答案:

答案 0 :(得分:0)

您可以覆盖点setter和getter方法,以在模型中获得所需的行为。

app/models/other_detail.rb

  def points
    self[:points]&.join(",")
  end

  def points=(values)
    self[:points] = values.split(",")
  end