mercredi 5 août 2015

Why is does delete not work on error message array?

I have the following code...

def register_learner
    @event = Event.find(params[:event_id])
    @registation = EventRegistration.new first_name: params[:first_name], last_name: params[:last_name], email: params[:email], event_id: params[:event_id]

    if !@registation.valid?
      @registation.errors.full_messages.delete("Event has already been taken")
      flash[:notice] = @registation.errors.full_messages.to_sentence
      redirect_to(event_path(@event))
    else
      @registation.save
      ...
  end

Note the line @registation.errors.full_messages.delete("Event has already been taken") where I am trying to delete this particular message from the full_messages array, however it does not work. The next line is the flash message, and the message "Event has already been taken" is still being displayed.

Here is a sanity check via the console...

2.1.5 :001 > errors = ["Event has already been taken", "Last name can't be blank"]
 => ["Event has already been taken", "Last name can't be blank"]
2.1.5 :002 > errors.delete "Event has already been taken"
 => "Event has already been taken"
2.1.5 :003 > errors
 => ["Last name can't be blank"]

What am I missing?

Aucun commentaire:

Enregistrer un commentaire