Rails Dropdown in Form Template References
The Rails scaffolded views do not generate a dropdown for selecting referenced data. Fortunately, this can be changed by editing the Rails scaffolding templates.
First, locate the railties gem:
find $(gem env home)/gems -name 'railties-[0-9].[0-9].[0-9].[0-9]'
cd $(find $(gem env home)/gems -name 'railties-[0-9].[0-9].[0-9].[0-9]' | head -1)/lib/rails/generators/erb/scaffold/templates
In the _form.html.erb.tt , add this into the attributes.each loop:
<% elsif attribute.reference? -%>
<%%= form.label :<%= attribute.column_name %>, style: "display: block" %>
<%%= form.collection_select :<%= attribute.column_name %>, <%= attribute.name.camelize %>.all, :id, :name, prompt: true %>
The above code snippet assumes that the master record has a name attribute - this assumption is the reason why Rails scaffolding doesn’t generate the dropdowns out-of-the-box.