我一直在尝试让一些动态选择功能正常工作,但是尽管有许多不同的教程,我还没有让它工作。为了便于阅读,我将代码示例归结为基础。任何建议将不胜感激。
在故障页面上,我需要为故障分配公司和联系人,但我只想能够看到与所选公司关联的联系人
Fault - belongs_to :company, :user, :contact
User - has_many :faults
Contacts - has_and_belongs_to_many :companies
Company - has_and_belongs_to_many :contacts, has_many :faults
/faults/_form.html.erb
<%= f.label :company, "Company:" %>
<%= collection_select(:fault,:company_id,@companies,:id,:full_name, :prompt => "Please select a company") %></br>
<%= f.label :contact, "Contact:" %>
<%= f.collection_select :contact_id, @contacts, :id, :name, :prompt => "Select a Contact" %>
<%= link_to "Add New Contact", {:controller => "companies", :action => "index"}, :confirm => "To add a contact for a company you need to do this from the companies page." %></br>