2017-05-01 5 views
0

aufzurufen Ich versuche, die Anzahl der Urlaubstage zu aktualisieren, die ein Mitarbeiter hat, wenn er einen Urlaub bucht. Ich versuche den Mitarbeiter von dem Modell zu retten, das scheinbar nicht funktioniert, der Urlaub schafft sich ansonsten perfekt. Ich hatte es vorher funktioniert, aber muss etwas aus Versehen geändert haben, egal was ich mache, ich kann es nicht wieder arbeiten. Irgendwelche Ideen? Controller-Ich versuche, die Speichermethode vom Modell

Urlaub

def create 
    @holiday = Holiday.new(holiday_params) 
    @holiday.employee_id = current_employee.id 

    if(Holiday.pastHol(@holiday)) 
    if(Holiday.h_authorize(current_employee, @holiday)) 
     if(Holiday.update_holidays(current_employee,@holiday)) 
     respond_to do |format| 
      if @holiday.save 
      format.html { redirect_to action: "index", notice: 'holiday accecpted' } 
      format.json { render :show, status: :created, location: @holiday } 
      end 
     end 
     else 
     respond_to do |format| 
      format.html { redirect_to action: "index", notice: 'holiday declined' } 
      format.json { render json: @holiday.errors, status: :unprocessable_entity } 
     end 
     end 
    else 
     respond_to do |format| 
     format.html { redirect_to action: "index", notice: "Already on Hols" } 
     format.json { render json: @holiday.errors, status: :unprocessable_entity } 
     end 
    end 
    else 
    respond_to do |format| 
     format.html { redirect_to action: "index", notice: "Date error" } 
     format.json { render json: @holiday.errors, status: :unprocessable_entity } 
    end 
    end 
end 

Ferien Modell

scope :update_holidays, lambda{ |q| where("amount > ?", q) } 
scope :proj_authorize, lambda{ |q| where("amount > ?", q) } 

def self.update_holidays(employee, holiday) 
    employee.DaysHolidays == employee.DaysHolidays - (holiday.endDate - holiday.startDate) - 1 

    if (employee.DaysHolidays > 0) 
    employee.save 
    return true 
    else 
    return false 
    end 
end 

Ferien Index

<%= form_for(@holiday) do |f| %> 
    <%= errors_for(@holiday) %> 

    <h3>Holiday Request</h3> 

    <%= current_employee.name %> , you have <%= current_employee.DaysHolidays %> holiday days remaining.<br><br> 

    <p id="notice"><%= notice %></p> 

    <div class="field"> 
    <%= f.label :startDate %><br> 
    <%= f.text_field :startDate , id: :datepicker, placeholder: "Start Date" %> 
    </div> 

    <div class="field"> 
    <%= f.label :endDate %><br> 
    <%= f.text_field :endDate , id: :datepicker1, placeholder: "End Date" %> 
    <div class="actions"> 
     <%= f.submit 'Request Holiday', :class => 'btn' %> 
    </div> 
<% end %> 
</form> 
</div> 
+0

Ersetzen 'save' mit' speichern 'an allen Orten und Sie erhalten eine Ausnahme sehen, ob eine Validierung –

+0

Dank fehlschlägt, Es Speicher nicht, weil ein Passwort kann nicht leer sein, gibt es eine Möglichkeit, dieses Feld zu ignorieren oder nur die Anzahl der Urlaubstage zu aktualisieren? – David16

+0

Ist dieser Fehler bei einem "Mitarbeiter" -Objekt? Erstellen Sie gleichzeitig ein "Mitarbeiter" -Objekt, wenn Sie einen "Feiertag" erstellen? –

Antwort

0

ich bemerkt habe, dass Sie bereits die Antwort auf Ihre Frage gefunden, aber ich wirklich Ich wollte deinen Controller etwas umgestalten. Während ich Ihnen rate, Ihre if Fälle zu einer Service-Klasse oder einem zugrunde liegenden Ansatz zu verschieben, sollte wahrscheinlich anders sein, aber ohne andere Informationen, kann ich so viel tun. Hier ist eine leichte refactor während alles in der Steuerung zu halten:

def create 
    @holiday = Holiday.new(holiday_params) 
    @holiday.employee_id = current_employee.id 
    notice = 
    if(Holiday.pastHol(@holiday)) 
     if(Holiday.h_authorize(current_employee, @holiday)) 
     if(Holiday.update_holidays(current_employee, @holiday)) 
      "holiday accepted" 
     else 
      "holiday declined" 
     end 
     else 
     "Already on Hols" 
     end 
    else 
     "Date error" 
    end 

    respond_to do |format| 
    format.html { redirect_to action: "index", notice } 
    if @holiday.save 
     format.json { render :show, status: :created, location: @holiday } 
    else 
     format.json { render json: @holiday.errors, status: :unprocessable_entity } 
    end 
    end 
end 
Verwandte Themen