Reputation: 1002
I having problem in render whole layout. In Controller new action, if the payslip was created already means, it will show message like already payslip created. otherwise it won't show anything. but it render the whole layout. This will happen when create a first payslip for teacher.
Controller
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;"> <img src="/assets/side_menu/payslip.png" alt="Home" ></i> Create Payslip</h4><br/><br/>
<div class="offset1">
<div id = "employee_main_info" >
<h3 style = "margin-left:150px;"><%= @teacher.first_name.capitalize %> (<%= @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>
it shows error like
Please help me..
Upvotes: 0
Views: 88
Reputation: 76774
Although I can't see why your .js.erb
file would render a layout, you should try using the :layout => false
call, like this:
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
Something else you'll want to take note of is the principle of thin controller, fat model
. Your controller is full of conditional logic which can easily be put into the model
I'd recommend looking into class functions, so you can call those functions instead of relying on so many lines of code in the controller action
Upvotes: 1