简体   繁体   中英

Rails_admin Dropdown menu with has_many, belongs_to models relations

Im building a rails admin dropdown with various different models in my app. The two models I want to link are Student and Company. Student belongs_to Company and Company has_many Student's. The code is as follows:

RailsAdmin.confing do |config|
...
config.model 'Student' do
   edit do
       ...
       various fields
       ...
       field connection_with_company, :enum do     
          connection_with_company
       ...

I already have created a method in the Student model returning the values I need but rails admin does not read the methods I make in the model. The idea is when adding a new student, list the available companies. I have searched and tried almost everything. I already tried custom field and virtual field, although there is a possibility I may have done it wrong. Any help will be much appreciated!! Thank you!

Update

Tried this but it does not appear on the edit section

$('#academy.id').parent().hide() academies = $('#academy.id').html() 
$('#company_profiles.company_id').change -> 
  companies = $('#company_profiles.company_id :selected').text()
  escaped_companies = companies.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1') 
  options = $(academies).filter("optgroup[label='#{companies}']").html()
  if options $('#academy.id').html(options)
    $('#academy.id').parent().show() 
  else 
    $('#academy.id').empty() $('#academy.id').parent().hide()

You can't configure rails admin to have one field values depend on the value of another field, at least not without first saving the object. You'd have to build a new field that combines both. Which path do you want? The chose a student save the object and then show the companies field with the filtered values? Or the sprinkle javascript magic on a brand new field that combines two?

Can you try adding this file?

app/assets/javascripts/rails_admin/custom/ui.js

//= require_tree .
//= require 'rails_admin/custom/yourfile.js'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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