简体   繁体   English

活动管理员编辑表单不预先填充belongs_to关系

[英]active admin edit form not pre-populating belongs_to relationship

Im using Rails 4 and I have a form within active admin that does not pre-populate or load relationship data into the edit form. 我使用Rails 4,我在活动管理员中有一个表单,不会预先填充或加载关系数据到编辑表单。 If I dont define a form then the default that is loaded by active admin does, but it is shows the object and not the object.name for example so I have set out to edit it. 如果我没有定义一个表单然后由活动管理员加载的默认值,但它显示对象而不是object.name,所以我已经开始编辑它。

Here is what I have 这就是我所拥有的

form do |c|
  c.semantic_errors *c.object.errors.keys
  c.inputs "Event" do
    c.input :title
    c.input :date, :as => :datetime_picker, :local => true 
    c.input :description
  end
  c.inputs "Training Request" do
    c.inputs :for => [:training_request, c.object.training_request || c.object.build_training_request] do |w|
      list_of_training_requests = TrainingRequest.fulfilled.collect {|t| t.host.name }
      w.input :id, as: :select, :collection => list_of_training_requests
    end
  end
  c.inputs "Trainer" do
    c.inputs :for => [:trainer, c.object.trainer || c.object.build_trainer] do |x|
      list_of_trainers = Trainer.qualified.collect {|t| t.name }
      x.input :id, as: :select, :collection => list_of_trainers
    end
  end
  c.actions
end

The form loads without any errors and does list data just not what is set the in the database eg 表单加载没有任何错误,并且列出的数据不是数据库中设置的数据,例如

在此输入图像描述

If anyone can point me in the right direction it would be very much appreciated. 如果有人能指出我正确的方向,我将非常感激。

If the only problem with the original forms is that it just displays the object instead of name you could overwrite to_s in your models. 如果原始表单的唯一问题是它只显示对象而不是名称,则可以覆盖模型中的to_s For example: 例如:

class Trainer < ActiveRecord::Base
  def to_s
    name
  end
end

Then when you call an instance of Trainer it should display the name property. 然后,当您调用Trainer实例时,它应显示name属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM