mercredi 13 décembre 2017

no mothed for nil after rendering view in controller

So i have this in my view _form.erb

<div class="form-group">
    <%= f.label :start_hour %><br>
    <%= f.select :start_hour, @select_hours.map {|value| [value, value]} %>
</div>

And this in edit.erb

<%= render 'form' %>

And this in my controller

    def edit
        @user = current_user
        @employee = @user.employee
        @hour = @employee.working_hours.find(params[:id])
        @select_hours = Array.new
        for i in 0..12
          @select_hours.push("#{07+i}:00")
          @select_hours.push("#{07+i}:30")
        end
    end

And then my update in my controller

def update
    @employee = current_user.employee
    @hour = @employee.working_hours.find(params[:id])
    if @hour.update(working_hour_params)
      redirect_to employee_working_hours_path(@employee)
    else
      render :edit
    end
end

And here's my problem: when i click update AND have wrong start_hour (custom validation, works when creating not editig) so @hour will not update it renders again this view but with error that there is no method .map for nil (so for @select_hours). So how can i do to fix this?

Aucun commentaire:

Enregistrer un commentaire