oninput event is not working in Rails form - ruby

I want to add oninput event in rails form using ajax call.I added it but its not working.I also want when input field will get the data the form will submit and result will come in the same page without reloading the page.I am explaining my code below.
homes/home.html.erb
<%= form_for :sdf ,:url => {:action => "scan_report" },remote: true do |f| %>
<% if params[:receipt_no] %>
<div class="input-group bmargindiv1 col-md-12"> <span class="input-group-addon text-left">Receipt No. Scan :</span>
<%= f.text_field :Receipt_No,:class => "form-control", :value => params[:receipt_no],oninput => "this.form.submit();" %>
</div>
<% else %>
<div class="input-group bmargindiv1 col-md-12"> <span class="input-group-addon text-left">Receipt No. Scan :</span>
<%= f.text_field :Receipt_No,:class => "form-control",placeholder:"Receipt No. scan" %>
</div>
<% end %>
<% end %>
The above input field will get value when user will click on view action of table which is given below.
reports/view_report.html.erb
<div class="block-content">
<div class="table-responsive">
<table class="table table-bordered">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-2 col-sm-2">
<col class="col-md-3 col-sm-3">
<col class="col-md-3 col-sm-3">
<col class="col-md-1 col-sm-1">
<col class="col-md-1 col-sm-1">
<col class="col-md-1 col-sm-1">
</colgroup>
<thead>
<tr>
<th class="text-center">Sl. No</th>
<th class="text-center">Receipt Number</th>
<th class="text-center">Date</th>
<th class="text-center">Deceased Name</th>
<th class="text-center">Deceased Adress</th>
<th class="text-center">Doner Name</th>
<th class="text-center">Doner Adress</th>
<th class="text-center">Doner Mobile No</th>
<th class="text-center">Doner's Relationship</th>
<th class="text-center">Donation Amount</th>
<th class="text-center">Date of death</th>
<th class="text-center">HCSY Status</th>
<th class="text-center">Deceased photo</th>
<th class="text-center">Doner photo</th>
<th class="text-center">Brahmin</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<% #sdf.each do |sdf| %>
<tr>
<td class="text-center"><%= sdf.Sdp_Id %></td>
<td class="text-center"><%= sdf.Receipt_No %></td>
<td class="text-center"><%= sdf.Created_On %></td>
<td class="text-center"><%= sdf.Deceased_Name %></td>
<td class="text-center"><%= sdf.Deceased_Address1 %></td>
<td class="text-center"><%= sdf.Doner_Name %></td>
<td class="text-center"><%= sdf.Doner_Address1 %></td>
<td class="text-center"><%= sdf.Doner_MobileNo %></td>
<td class="text-center"><%= sdf.Doner_Relationship_Other %></td>
<td class="text-center"><%= sdf.Donation_Amount %></td>
<td class="text-center"><%= sdf.Date_Of_Death %></td>
<td class="text-center"><%= sdf.HCSY_Status %></td>
<td class="text-center"><%= image_tag(sdf.Photo,:width => 50,:height => 50) %></td>
<td class="text-center"><%= image_tag(sdf.Doner_Photo,:width => 50,:height => 50) %></td>
<td class="text-center"><%= sdf.Brahmin %></td>
<td class="text-center">
<div class="btn-group">
<a href="/homes/home/?receipt_no=<%= sdf.Receipt_No %>" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="View">
<i class="fa fa-eye"></i>
</a>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate #sdf %>
</div>
</div>
My other files are given below.
controller/homes_controller.rb
class HomesController < ApplicationController
def home
#sdf=TSdf.new
respond_to do |format|
format.html
format.js
end
end
def scan_report
if #sdf=TSdf.find_by_Receipt_No(params[:sdf][:Receipt_No])
#hcsy=THcsy.find_by_Sdp_Id(#sdf.Sdp_Id)
#hcsy_deatils=THcsyDetails.find_by_HCSY_ID(#hcsy.id)
#woods=THcsyFundTypeMaster.find_by_Fund_Type_Code(1)
#burn=THcsyFundTypeMaster.find_by_Fund_Type_Code(2)
#good=THcsyFundTypeMaster.find_by_Fund_Type_Code(3)
#swd=THcsyFundTypeMaster.find_by_Fund_Type_Code(5)
#photo=THcsyFundTypeMaster.find_by_Fund_Type_Code(6)
flash[:notice]=" number matched"
else
splitted = params[:sdf][:Receipt_No].split('/')
receipt = splitted[0]
table_id = splitted[1]
#sdfs=TSdf.find_by_Receipt_No(receipt)
if #sdfs
#hcsys=THcsy.find_by_Sdp_Id(#sdfs.Sdp_Id)
#fund_details=THcsyFundTypeMaster.find_by_Fund_Type_Code(table_id)
else
flash[:notice]="Scan number not found"
end
end
end
end
scan_report.js.erb
$("#whole-div1").css("display", "block");
$("#whole-div").css("display", "block");
$("#whole-div3").css("display", "block");
$("#whole-div4").css("display", "block");
$("#whole-div5").css("display", "block");
$("#whole-div6").css("display", "block");
$("#whole-div8").css("display", "block");
$(".flash-message").html('<%= escape_javascript flash[:notice] %>');
$("#sdf-puri").html("<%= escape_javascript (render 'sdf_output_table' ) %>");
$("#sdf-puri").slideDown(350);
$("#hcsy-puri").html("<%= escape_javascript (render 'hcsy_output_table' ) %>");
$("#hcsy-puri").slideDown(350);
$("#good-puri").html("<%= escape_javascript (render 'goods' ) %>");
$("#good-puri").slideDown(350);
$("#swd-puri").html("<%= escape_javascript (render 'swd' ) %>");
$("#swd-puri").slideDown(350);
$("#burn-puri").html("<%= escape_javascript (render 'burning' ) %>");
$("#burn-puri").slideDown(350);
$("#wood-puri").html("<%= escape_javascript (render 'woods' ) %>");
$("#wood-puri").slideDown(350);
$("#photo-puri").html("<%= escape_javascript (render 'photo' ) %>");
$("#photo-puri").slideDown(350);
Please help me to add this type of feature and its very important that result should come the same page without reloading the page.

Related

Display notification if search is not found using Rails 3

I am using will_paginate gem for searching data from table as per two date.Here my requirement is If no search data is found there should be one notification to user i.e-"No record found in between these two date.".Mu code files are given below.
view_report.html.erb:
<div class="col-md-4 col-md-offset-1">
<div class="text-left">
To
</div>
<div class="input-group bmargindiv1 date" id='datepicker1' data-date="2015-05-01" data-date-format="yyyy-mm-dd">
<%= text_field_tag :search1, params[:search1], :class => "form-control" %>
<span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
<script type="text/javascript">
$('#datepicker1').datepicker({
format: "yyyy-mm-dd"
});
</script>
</div>
<%= submit_tag "Search",:class => "btn btn-info btn-lg" %>
<div class="clearfix"></div>
<% end %>
</div>
<!--1st_total_div-->
<!--1st_total_div-->
<div class="block-content">
<div class="table-responsive">
<table class="table table-bordered">
<colgroup>
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
<col class="col-new-md-1">
</colgroup>
<thead>
<tr>
<th class="text-center">Sl. No</th>
<th class="text-center">Receipt<br>Number</th>
<th class="text-center">Date</th>
<th class="text-center">Deceased<br>Name</th>
<th class="text-center">Doner<br>Name</th>
<th class="text-center">Doner<br>Mobile No</th>
<th class="text-center">Doner's<br>Relationship</th>
<th class="text-center">Donation<br>Amount</th>
<th class="text-center">Date of<br>death</th>
<th class="text-center">HCSY<br>Status</th>
<th class="text-center">Brahmin</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<% #sdf.each do |sdf| %>
<% #count=#count+1 %>
<tr>
<td class="text-center"><%= #count %></td>
<td class="text-center"><%= sdf.Receipt_No %></td>
<td class="text-center"><%= sdf.Created_On %></td>
<td class="text-center"><%= sdf.Deceased_Name %></td>
<td class="text-center"><%= sdf.Doner_Name %></td>
<td class="text-center"><%= sdf.Doner_MobileNo %></td>
<td class="text-center"><%= sdf.Doner_Relationship_Other %></td>
<td class="text-center"><%= sdf.Donation_Amount %></td>
<td class="text-center"><%= sdf.Date_Of_Death %></td>
<td class="text-center"><%= sdf.HCSY_Status %></td>
<td class="text-center"><%= sdf.Brahmin %></td>
<td class="text-center">
<div class="btn-group">
<a href="/homes/home/?receipt_no=<%= sdf.Receipt_No %>" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="View">
<i class="fa fa-eye"></i>
</a>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= will_paginate #sdf %>
</div>
</div>
<!--end_1st_total_div-->
<!--end_1st_total_div-->
</div>
reports_controller.rb:
class ReportsController < ApplicationController
helper_method :sort_column, :sort_direction
def view_report
#count=0
#sdf =TSdf.all
#sdf = TSdf.search(params[:search],params[:search1]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page])
end
def sort_column
TSdf.column_names.include?(params[:sort]) ? params[:sort] : "Created_On"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end
t_sdf.rb:
class TSdf < ActiveRecord::Base
# attr_accessible :title, :body
self.table_name = 'T_SDF'
def self.search(search,search1)
if search && search1
#where('Created_On LIKE ?', "%#{search}%")
where("Created_On >= ? AND Created_On <= ?", search, search1 )
else
scoped
end
end
end
Please help me to add this notification as per the requirement.
Try this:
<% if #sdf.blank? %>
<p>No record found in between these two date.</p>
<% else %>
<table class="table table-bordered">
....
<thead>
....
</thead>
<tbody>
<% #sdf.each do |sdf| %>
....
<% end %>
</tbody>
</table>
<%= will_paginate #sdf %>
<% end %>

Check whether the check-box is checked or not using Rails 3

I have a check-box inside a form.I need if user will checked this check-box suddenly the action will execute and it will check the check-box status whether it is checked or not using Rails 3.My form is given below.
paym.html.erb:
<%= form_for :add_payment,:url => {:action => 'add_payment' },remote: true do |f| %>
<table class="table table-bordered">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-1 col-sm-1">
<col class="col-md-3 col-sm-3">
<col class="col-md-3 col-sm-3">
<col class="col-md-4 col-sm-4">
</colgroup>
<thead>
<tr>
<th class="text-center"><input type="checkbox"></th>
<th class="text-center">Sl. No</th>
<th class="text-center">Date</th>
<th class="text-center">Receipt No.</th>
<th class="text-center">Amount</th>
</tr>
</thead>
<tbody>
<% #result.each do |r| %>
<tr>
<th class="text-center"><%= check_box_tag :check_value,nil,:id => "checkbox1-1" %></th>
<td class="text-center"><%= r.id %></td>
<td class="text-center"><%= r.c_date %></td>
<td class="text-center"><%= r.Receipt_No %></td>
<td class="text-center"><i class="fa fa-rupee"></i><%= r.v_amount %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
paym_controller.rb:
class PaymentsController < ApplicationController
def add_payment
end
end
Here i have a table with some value .I also need when user will checked that box 1-It will check whether check box is checked or not,2-If checked then it will get all table value corresponding to that check-box.Please help me.
<%= check_box_tag :check_value,nil,:id => "checkbox1-1" %>
You had pass nil value instead pass the object r like:
<%= check_box_tag :check_value, :value => r, :id => "checkbox1-1" %>
Now using jquery you can check if checkbox is checked then retrieve it's value. In form of value you will get particular object r and from that you can fetch your required data.
I hope this may helps you :)

Cannot retrieve a column data using Rails 3

I want to access a column of data using Rails 3 but unable to retrieve it.Finally an empty array is coming while rendering the out put table.Please check my table first which is given below.
table- Payment_vendors:
id Receipt_No type v_amount v_date c_date v_name v_status
7 150325006 SWD 60.00 2015-04-15 2015-04-28 Deepak No
8 150325006 GOODS 1195.00 2015-04-15 2015-04-28 Deepak No
From the above table i want to access "type" column by using the v_name.I tried once but got the empty array.I explaining my code below.
payment.html.erb:
<div class="tbpaddingdiv2">
<%= form_for :payment,:url => {:action => "check_type" },remote: true do |f| %>
<div class="totalaligndiv">
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Type :</div></span>
<%= f.select(:s_catagory,options_for_select([['Wood','Woods'],['Puja Samagree','GOODS'],['Sweeper','SWD'],['Photo Grapher','PHOTO'],['Burning Assistant','BURNING'],['BRAHMIN','BRAHMIN']],selected: "Type"),{},{:class => 'form-control',:onchange => ("$('#switch_car').submit()")}) %>
</div>
<div id="div_select" style="display:none">
<div class="input-group bmargindiv1 col-md-6 pull-left" ><span class="input-group-addon text-left" ><div class="leftsidetextwidth">Select Vendor :</div></span>
<div id="name-option">
</div>
</div>
</div>
<div class="clearfix"></div>
<div class="tbpaddingdiv1 text-center">
<%= f.submit "submit",:class => "btn btn-success",:id => "switch_car" %>
</div>
</div>
<% end %>
</div>
payments_controller.rb:
class PaymentsController < ApplicationController
def payment
#payment=Vendor.new
respond_to do |format|
format.html
format.js
end
end
def check_type
if params[:commit]=="submit"
#vendor_type=PaymentVendor.where(:v_name => params[:payment][:v_name] ).pluck(:type)
else
#v_name=Vendor.where(:s_catagory => params[:payment][:s_catagory] ).pluck(:v_name)
end
end
end
check_type.js.erb:
<% if #v_name %>
$("#div_select").css("display", "block");
$("#name-option").html("<%= escape_javascript (render 'nameoption' ) %>");
$("#name-option").slideDown(350);
<% end %>
<% if #vendor_type %>
console.log('hello')
$(".flash-message").html('<%= escape_javascript flash[:notice] %>');
$("#paymentdetail").css("display", "block");
$("#paymentoption").html("<%= escape_javascript (render 'paymentdetails' ) %>");
$("#paymentoption").slideDown(350);
<% end %>
After getting all column data i need when type=="SWD" ,all related value of this row will display in below table.
paymentdetails.html.erb:
<table class="table table-bordered">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-1 col-sm-1">
<col class="col-md-3 col-sm-3">
<col class="col-md-3 col-sm-3">
<col class="col-md-4 col-sm-4">
</colgroup>
<thead>
<tr>
<th class="text-center"><input type="checkbox"></th>
<th class="text-center">Sl. No</th>
<th class="text-center">Date</th>
<th class="text-center">Receipt No.</th>
<th class="text-center">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<th class="text-center"><input type="checkbox" id="checkbox1-1" name="checkbox1-1"></th>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"><i class="fa fa-rupee"></i></td>
</tr>
</tbody>
</table>
Please help me to resolve this issue.
You are trying to pass params[:payment][:v_name] instead params[:v_name]
Try this: in your controller's check_type method:
#vendor_type = PaymentVendor.where(:v_name => params[:v_name] ).pluck(:type)

How to get hidden field array value using Rails 3

I have a hidden field in my table.I need to access all array value into controller's action method in rails.Check my below table.
paymentresult.html.erb:
<%= form_for :add_payment,:url => {:action => 'add_payment' },remote: true do |f| %>
<table class="table table-bordered">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-1 col-sm-1">
<col class="col-md-3 col-sm-3">
<col class="col-md-3 col-sm-3">
<col class="col-md-4 col-sm-4">
</colgroup>
<thead>
<tr>
<th class="text-center"><input type="checkbox"></th>
<th class="text-center">Sl. No</th>
<th class="text-center">Date</th>
<th class="text-center">Receipt No.</th>
<th class="text-center">Amount</th>
</tr>
</thead>
<tbody>
<% #result.each do |r| %>
<%= hidden_field_tag 'infos[]', r %>
<tr>
<th class="text-center"><%= check_box_tag :check_value, 1, :id => "checkbox1-1" %></th>
<td class="text-center"><%= r.id %></td>
<td class="text-center"><%= r.c_date %></td>
<td class="text-center"><%= r.Receipt_No %></td>
<td class="text-center"><i class="fa fa-rupee"></i><%= r.v_amount %></td>
</tr>
<% end %>
</tbody>
</table>
<%= f.submit "Add to payment",:class => "btn btn-success",:id => "switch_car11" %>
<% end %>
payment_controller.rb
class PaymentsController < ApplicationController
def add_payment
if params[:commit]
params[:check_value] == '1' ? remember : forget
else
if params[:add_payment][:p_catagory]=="Cheque"
#chk=true
end
end
end
private
def remember
#reslt=params[:infos]
#add_payment=AddPayment.create(:p_catagory => params[:add_payment][:p_catagory],:paid_amount => "here i want to add the table's v_amount value",:total_claim => 1 )
end
end
add_payment.rb:
class AddPayment < ActiveRecord::Base
attr_accessible :chk_details, :chk_no, :p_catagory, :paid_amount, :total_claim
end
I need when user will click on submit button then all array value(i.e-#result) should fetch to add_payment method.I tried by using hidden field but failed to fetch all data.Here my requirement is after submitting the form all value of #resultshould fetch into controller method so that i can save some value from this in DB.

How to check check box true or false in rails 3

I have a check box. I need when this check box is checked then the controller action will get all the related data, if it is not checked it will not.
<table class="table table-bordered">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-1 col-sm-1">
<col class="col-md-3 col-sm-3">
<col class="col-md-3 col-sm-3">
<col class="col-md-4 col-sm-4">
</colgroup>
<thead>
<tr>
<th class="text-center"><input type="checkbox"></th>
<th class="text-center">Sl. No</th>
<th class="text-center">Date</th>
<th class="text-center">Receipt No.</th>
<th class="text-center">Amount</th>
</tr>
</thead>
<tbody>
<% #result.each do |r| %>
<%= hidden_field_tag 'infos[]',r.v_amount %>
<tr>
<th class="text-center"><%= check_box_tag "deatils[]" ,r.v_amount,{} ,:id => r.id %></th>
<td class="text-center"><%= r.id %></td>
<td class="text-center"><%= r.c_date %></td>
<td class="text-center"><%= r.Receipt_No %></td>
<td class="text-center"><i class="fa fa-rupee"></i><%= r.v_amount %></td>
</tr>
<% end %>
</tbody>
</table>
Please help me to resolve this issue.
Add class 'related_data' to the checkbox field
<%= check_box_tag "deatils[]" ,r.v_amount,{} ,:id => r.id,class: 'related_data' %>
Then write a coffee like this:
jQuery(".related_data").live 'change', ->
jQuery.ajax '/controller/action_to_get_data',
type: 'POST'
I hope you get an idea to solve your problem.

Resources