56

我在我的项目中使用ActiveAdmin gem。

我有 2 个模型通过关联使用 has_many。数据库模式看起来与 RailsGuide 中的示例完全相同。http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association (来源:rubyonrails.orghas_man 通过关联

如何使用 ActiveAdmin 来...

  1. 在医生页面中显示每位患者的预约日期?
  2. 在医生页面编辑每位患者的预约日期?

谢谢大家。:)

4

7 回答 7

84

对于 1)

show do
  panel "Patients" do
    table_for physician.appointments do
      column "name" do |appointment|
        appointment.patient.name
      end
      column :appointment_date
    end
  end
end

对于 2)

form do |f|
  f.inputs "Details" do # physician's fields
    f.input :name
  end

  f.has_many :appointments do |app_f|
    app_f.inputs "Appointments" do
      if !app_f.object.nil?
        # show the destroy checkbox only if it is an existing appointment
        # else, there's already dynamic JS to add / remove new appointments
        app_f.input :_destroy, :as => :boolean, :label => "Destroy?"
      end

      app_f.input :patient # it should automatically generate a drop-down select to choose from your existing patients
      app_f.input :appointment_date
    end
  end
end
于 2011-08-18T02:45:16.017 回答
15

在回答tomblomfield的评论中跟进问题:

在您的 AA ActiveAdmin.register 模型执行块中尝试以下操作:

  controller do
    def scoped_collection
      YourModel.includes(:add_your_includes_here)
    end
  end

这应该在单独的查询中延迟加载每个索引页面的所有关联

高温高压

于 2012-01-27T14:39:00.127 回答
1

它应该解决 N+1 查询问题。

show do
  panel "Patients" do
    patients = physician.patients.includes(:appointments)
    table_for patients do
      column :name
      column :appointment_date { |patient|    patient.appointments.first.appointment_date }
    end
  end
end
于 2015-06-19T14:04:07.627 回答
1

这对我有用(选择)

permit_params category_ids: []

form do |f|
   inputs 'Shop' do
     input :category_ids, collection: Category.all.collect {|x| [x.name, x.id]}, as: :select, multiple: true, input_html: { class: "chosen-input",  style: "width: 700px;"}
    end
   f.actions
end
于 2017-03-03T14:20:17.117 回答
0

@monfresh @tomblomfield 你可以做

has_many :appointments, ->{ includes(:patients) }, :through => :patients

在医生模型中

...或者,我不确定您是否可以将它与 formtastic 一起使用,但您可以使用类似的东西将范围设为可选

has_many :appointments :through => :patients do
  def with_patients
    includes(:patients)
  end
end

并且appointment.patient不会 n+1 了

于 2015-01-19T22:56:09.383 回答
0

关于#2,它应该是这样的:

form do |f|
  f.inputs 'Physician Details' do
    f.input :name
  end

  f.inputs 'Physician Appointments' do
    f.has_many :appointments,
               heading: false,
               new_record: 'Add new appointment',
               remove_record: 'Delete appointment',
               allow_destroy: true do |app|
    app.input :patient, label: 'Choose the patient', collection: Patient.pluck(:name, :id)
    app.input :appointment_date
  end
end

关于标题: - 它可以是假的或一些标签(字符串)

关于allow_destroy: -您可以设置它检查用户管理员权限,如此处所示

重要- 在医师模型中,确保有

accepts_nested_attributes_for :appointments, allow_destroy: true

并且,在活动管理模型文件 - admin\physicians.rb - 中设置:

permit_params :name, appointments_attributes: [:patient_id, :_destroy, :id]
于 2021-03-19T19:41:27.843 回答
0

如果您想在面板行中显示多个字段,您可以使用以下视图:

show do |phy|
   panel "Details" do
        attributes_table do
          ... # Other fields come here
          row :appointment_dates do
            apps=""
            phy.appointments.all.each do |app|
              apps += app.patient.name + ":" + app.appoinment_date + ", "
            end
            apps.chomp(", ")
          end
        end      
   end
end

要将其放入您的 redit 表单中,请先将 Appointment_ids 放入允许列表:

permit_params: appointment_ids:[]

添加与表单有很多关系

form do |f|
   f.has_many :appointments do |app|
     app.inputs "Appointments" do
       app.input :patients, :as => :select, :label => "Assigned Patients"
       app.input :appointment_date
     end  
   end
end

如果没有编码错误,应该可以工作。

于 2016-08-26T22:17:25.337 回答