2017-05-01 3 views
0

휴가를 예약 할 때 직원의 휴일 일 수를 업데이트하려고합니다. 나는 일하는 것 같지 않은 모델로부터 직원을 구하려고 노력하고있다. 휴일은 완벽하게 그렇지 않다. 전에 작업 했었지만 실수로 무엇인가를 변경 했음에 틀림이 없습니다. 어떤 아이디어?모델에서 save 메소드를 호출하려고 시도했습니다.

휴일 컨트롤러

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 

휴일 모델

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 

휴일 당신이 이미 귀하의 질문에 대한 답을 발견 한 것으로 나타났습니다

<%= 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

가'와'save' 교체 저장 '모든 장소와 검증이 –

+0

감사를 실패 할 경우 예외를 볼 암호를 할 수 있기 때문에 비어 있지 않거나,이 필드를 무시하거나 휴일 수를 업데이트 할 수있는 방법이 있습니까? – David16

+0

일부 'Employee' 객체에서이 오류가 발생합니까? 'Holiday'를 만들 때'Employee' 객체를 동시에 생성하고 있습니까? –

답변

0

지수,하지만 난 정말 컨트롤러를 약간 리팩토링하고 싶었습니다. if 케이스를 서비스 클래스로 옮기거나 기본 접근법을 다른 정보로 옮겨야한다고 충고하지만, 나는 그렇게 많이 할 수 있습니다. ! 여기에 약간의 리팩토링은 컨트롤러의 모든 것을 유지하면서 수 있습니다 : 그것은 절약 아니에요,

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 
관련 문제