无法使用 Rails 检索列数据 3
Cannot retrieve a column data using Rails 3
我想使用 Rails 3 访问一列数据,但无法检索 it.Finally 呈现输出时出现空数组 table.Please 检查我的 table 首先是下面给出的。
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
从上面的 table 我想访问 "type" 列通过使用 v_name.I 尝试过一次但是得到空 array.I 解释我的代码。
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 %>
当type=="SWD"获取我需要的所有列数据后,该行的所有相关值将显示在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>
请帮我解决这个问题。
您正试图通过 params[:payment][:v_name]
而不是 params[:v_name]
试试这个:在控制器的 check_type
方法中:
@vendor_type = PaymentVendor.where(:v_name => params[:v_name] ).pluck(:type)
我想使用 Rails 3 访问一列数据,但无法检索 it.Finally 呈现输出时出现空数组 table.Please 检查我的 table 首先是下面给出的。
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
从上面的 table 我想访问 "type" 列通过使用 v_name.I 尝试过一次但是得到空 array.I 解释我的代码。
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 %>
当type=="SWD"获取我需要的所有列数据后,该行的所有相关值将显示在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>
请帮我解决这个问题。
您正试图通过 params[:payment][:v_name]
而不是 params[:v_name]
试试这个:在控制器的 check_type
方法中:
@vendor_type = PaymentVendor.where(:v_name => params[:v_name] ).pluck(:type)