0

我是新来的轨道。我的工资单控制器有问题。使用它,我们可以为每个员工创建工资单。问题是一旦条件为真,它就会显示工资单已经创建。如果失败,它会再次呈现整个表单。

在控制器中

def new
@teacher_payslip = TeacherPayslip.new
@teacher = Teacher.find(params[:teacher_id])
if params[:date] and TeacherPayslip.find_by_teacher_id(params[:teacher_id]).present?
  @old_salary_date = TeacherPayslip.find_by_teacher_id(params[:teacher_id]).salary_date
  @a = @old_salary_date.strftime("%b%Y")
  @new_salary_date = params[:date].to_date
  @b = @new_salary_date.strftime("%b%Y")
  if @a == @b
    respond_to do |format|
      format.js
    end
  end
end

结尾

js文件

$('body').on("change","#teacher_payslip_salary_date",function(){
  var month = $('#teacher_payslip_salary_date').val();
  var teacher = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&')[0];
  var url = '/teacher_payslips/new?date='+ month +'&'+teacher
  $.ajax({
        url: url,
        dataType: 'html',
        type: 'GET',
        success: function(data){
        $('#payslip').html(data);
   }
  });
})

在此处输入图像描述

请指导我。

4

1 回答 1

0

将控制器的操作更改为:

def new
@teacher_payslip = TeacherPayslip.new
@teacher = Teacher.find(params[:teacher_id])
if params[:date] and TeacherPayslip.find_by_teacher_id(params[:teacher_id]).present?
  @old_salary_date = TeacherPayslip.find_by_teacher_id(params[:teacher_id]).salary_date
  @a = @old_salary_date.strftime("%b%Y")
  @new_salary_date = params[:date].to_date
  @b = @new_salary_date.strftime("%b%Y")
  if @a == @b
    respond_to do |format|
      format.js
    end
  else
    render nothing: true
  end
end
于 2013-10-10T09:40:19.233 回答