I’m trying to create a select field for a form that selects based on records selected for a model (called “Cancellation_Reasons”).
In my model called Cancellation:
<%= form_for(@cancellation do |f| %>
<%= options_from_collection_for_select(@cancellation_reasons, :id, :name) %>
<% end %>
In the Cancellation_Controller:
def new
@cancellation = Cancellation.new
@cancellation_reasons = CancellationReason.find(1)
respond_to do |format|
format.html # new.html.erb
format.json { render json: @trade }
end
end
When I run CancellationReason.find(1) in the the Rails Console it finds the record, so @cancellation_reasons isn’t nil. I think that it’s probably in how I’m using the select helpers (I’ve tried experimenting with them, but I’m not quite sure which one to use even after reading the Rails Guide and Rails API docs).
options_from_collection_for_select expect a collection (even it it is a collection of 1).
So change the code to be: