56

プロジェクトでActiveAdmingemを使用しています。

関連付けを通じてhas_manyを使用する2つのモデルがあります。データベーススキーマは、RailsGuideの例とまったく同じように見えます。http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association (ソース:rubyonrails.org関連付けによるhas_many

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.registerModeldoブロックで次のことを試してください。

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

これにより、各インデックスページのすべての関連付けを個別のクエリで遅延ロードする必要があります

HTH

于 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

パネル行に複数のフィールドを表示する場合は、次のビューを使用できます。

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 に答える
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

見出しについて: -falseまたは何らかのラベル(文字列)である可能性があります

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 に答える