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

时间:2015-09-27 15:38:04

标签: ruby-on-rails activeadmin

我正在使用Rails 4,并且我在活动管理员中有一个表单,它不会预先填充或将关系数据加载到编辑表单中。如果我没有定义表单然后由活动管理员加载的默认值,但它显示对象而不是object.name,例如我已经开始编辑它。

这就是我所拥有的

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

表单加载时没有任何错误,并且列出的数据不是数据库中设置的数据,例如。

operator <<

如果有人能指出我正确的方向,我们将非常感激。

1 个答案:

答案 0 :(得分:3)

如果原始表单的唯一问题是它只显示对象而不是名称,则可以覆盖模型中的to_s。例如:

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

然后,当您调用Trainer的实例时,它应显示名称属性。