1
我想使用Rails 3訪問一列數據,但無法檢索它。最後在呈現輸出表時最後會出現一個空數組。請首先檢查我的表, 。無法使用Rails 3檢索列數據
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
從上面的表格,我想通過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 %>
得到所有列數據後,我需要當鍵入==「SWD」時,該行的所有相關值將顯示在下表中。
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>
請幫我解決這個問題。
什麼是確切的錯誤您收到?請參閱[如何提出一個好問題]的指導原則(http://stackoverflow.com/help/how-to-ask) – screenmutt
@screenmutt:實際上,我正在獲取模板上的空數組。 – satya
你已經在這裏包含了很多不相關的信息。採取以下步驟(1)手動檢查你的表格,它是否有數據? (2)檢查控制器中命令的輸出,它是否爲空陣列? (3)刪除所有不相關的代碼(即表格和表格)。一旦你有數據被拉,你可以在任何地方使用它。 – screenmutt