error handling, Rails 4

Asked

Viewed 240 times

1

This error happens when the user does not select the option from the select field. The error would even be dealt with if it had not 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 dealt with:

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

1 answer

1


You’re not carrying the @disciplinas in the create. As it gives some validation error it tries to render again the view, but @disciplinas is empty.

Load it like you must have done in action new

Browser other questions tagged

You are not signed in. Login or sign up in order to post.