This error happens when the user does not select the option of the select field. The error up would be handled if it did not have the collect:
NoMethodError in Subdisciplinas#create
Showing _form.html.erb where line #18 raised:
undefined method 'collect' for nil:NilClass
<%= f.select :disciplina_id,
options_for_select(
@disciplinas.collect { |disciplina| [disciplina.nome.titleize, disciplina.id] }, @subdisciplina.disciplina_id), {prompt: 'Selecionar disciplina'}, { id: 'disciplinas_select' } %>
</div>
Controller where I believe the error should be handled:
def create
@subdisciplina = Subdisciplina.new(subdisciplina_params)
respond_to do |format|
if @subdisciplina.save
format.html { redirect_to @subdisciplina, notice: 'Subdisciplina was successfully created.' }
format.json { render :show, status: :created, location: @subdisciplina }
else
format.html { render :new }
format.json { render json: @subdisciplina.errors, status: :unprocessable_entity }
end
end
end