如何修复控制器的新功能渲染功能

时间:2013-10-10 09:36:32

标签: javascript ruby-on-rails

我是铁杆新手。我在payslip控制器有问题。使用这个我们可以为每个员工创建工资单。问题是,一旦条件成立,就会显示已经创建了payslip。如果失败,它会再次呈现整个表单。

在控制器中

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);
   }
  });
})

enter image description here

请指导我。

1 个答案:

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