2013-12-21 44 views
0

我在渲染整個佈局時遇到問題。在Controller新行動中,如果工資單已經創建,意味着它將顯示消息,如已創建的工資單。否則它不會顯示任何東西。但它呈現整個佈局。爲老師創建第一個工資單時會發生這種情況。如何解決在rails 4中整個佈局渲染問題

控制器

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.where(:teacher_id => params[:teacher_id]) 
     @new_salary_date = params[:date].to_date 
     @b = @new_salary_date.strftime("%b%Y") 
     @payslip = Array.new 
     @old_salary_date.each do |i| 
     a = i.salary_date.strftime("%b%Y") 
     @payslip << a 
     end 
     if @payslip.include?(@b) 
     @payslip_detail = TeacherPayslip.where("MONTH(salary_date) = ? and YEAR(salary_date) = ? and teacher_id = ? ",@new_salary_date.month, @new_salary_date.year,params[:teacher_id]).first 
     flash[:err] = "Payslip already Created" 
     respond_to do |format| 
     format.js 
     end 
     else 
     render nothing: true 
     end 
    end 
    end 

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

_form.html.erb

<%= simple_form_for @teacher_payslip, html: {class: 'form-inline form-horizontal'}, :validate => true do |f|%> 
<div class="tabable well"> 
     <div class="tab-content"> 
      <div class="tab-pane active"> 
       <div class="inputs"> 
       <h4 class="" style="border-bottom: 1px solid #dcdcdc;">&emsp;<img src="/assets/side_menu/payslip.png" alt="Home" ></i>&nbsp;&nbsp;Create Payslip</h4><br/><br/> 
       <div class="offset1"> 
       <div id = "employee_main_info" > 
       <h3 style = "margin-left:150px;"><%= @teacher.first_name.capitalize %>&nbsp;(<%= @teacher.teacher_code %>)</h3> 
       </div><br/> 
       <div class="inputs"> 
       <%= f.input :teacher_id, as: 'hidden', input_html: {class: '', value: @teacher.id} %> 
       <%= f.input :salary_date,as: 'string', input_html: {class: 'datepicker'} %> 
       <span id="payslip"></span> 
       <%= f.input :basic,label: "BASIC", :as => "string"%> 
       <%= f.input :hra, label: "HRA(%)", :as => "string" %> 
       <%= f.input :bonus, label: "Bonus(%)", :as => "string" %> 
       <%= f.input :pf, label: "PF(%)", :as => "string" %> 
       <%= f.input :da, label: "DA(%)", :as => "string" %> 
       <%= f.input :special_allowance, label: "Special Allowance", :as => "string" %> 
       </div> 
       </div> 
       <div class="form-actions"> 
       <%= button_tag(type: 'submit', class: "btn btn-primary error offset1") do %> 
       <i class="icon-ok icon-white"></i> Save 
       <% end %> 
       <%= link_to 'Back', teacher_list_path, class: 'btn btn-inverse animated rotateInDownRight' %> 
       </div> 
       </div> 
      </div> 
     </div> 
    </div> 
<% end %> 

new.js.erb

<div class="container"> 
<div class="row"> 
<div class="span4"> 
<div class="alert"> 
    <a class="close" data-dismiss="alert">×</a> 
    <strong><span id="payslips">Payslip Already Created for <%= link_to @payslip_detail.teacher.first_name,payslip_detail_path(id: @payslip_detail.id) %></span></strong> 
</div> 
</div> 
</div> 
</div> 

它顯示錯誤,如

enter image description here

請幫助我..

回答

1

雖然我不明白爲什麼你.js.erb文件將呈現佈局,您應該嘗試使用:layout => false來電,如下所示:

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.where(:teacher_id => params[:teacher_id]) 
      @new_salary_date = params[:date].to_date 
      @b = @new_salary_date.strftime("%b%Y") 
      @payslip = Array.new 
      @old_salary_date.each do |i| 
      a = i.salary_date.strftime("%b%Y") 
      @payslip << a 
      end 
      if @payslip.include?(@b) 
      @payslip_detail = TeacherPayslip.where("MONTH(salary_date) = ? and YEAR(salary_date) = ? and teacher_id = ? ",@new_salary_date.month, @new_salary_date.year,params[:teacher_id]).first 
      flash[:err] = "Payslip already Created" 
      respond_to do |format| 
      format.js { render :layout => false } 
      end 
      else 
      render nothing: true 
      end 
     end 
     end 

你想要注意的是thin controller, fat model的原理。控制器是完全可以很容易地放入模型

我建議尋找到class functions條件邏輯的,這樣你就可以調用這些函數,而不是依靠碼這麼多的線路中的控制器動作的