试图从模型中调用save方法

时间:2017-05-01 10:05:49

标签: ruby-on-rails ruby

我正在尝试更新员工预订假期时的假日天数。我正试图将员工从似乎没有工作的模型中拯救出来,否则假期会完美无缺。我之前有过工作,但一定是错误地改变了一些东西,因为无论我做什么我都无法恢复工作。任何想法?

假期控制器

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>

1 个答案:

答案 0 :(得分: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