Как исправить проблему с рендерингом макета в рельсах 4

0

У меня проблема в рендеринге всего макета. В новом контроллере Controller, если уже был создан 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.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>  

он показывает ошибку, как

Изображение 174551

Пожалуйста, помогите мне..

Теги:

1 ответ

1

Хотя я не могу понять, почему ваш файл .js.erb будет отображать макет, вы должны попробовать использовать :layout => false call, например:

  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. Ваш контроллер полон условной логики, которую можно легко ввести в модель

Я бы рекомендовал изучить функции класса, поэтому вы можете вызывать эти функции вместо того, чтобы полагаться на столько строк кода в действии контроллера

Ещё вопросы

Сообщество Overcoder
Наверх
Меню