如何使用 Rails Ajax 调用一次将两个对象传递给模板
How to pass two object at a time to the template using Rails Ajax call
我想将两个对象传递给模板,模板将在 Ajax 调用后使用 Rails 3.See 我下面的控制器文件进行渲染。
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
@sdf=TSdf.find_by_Receipt_No(params[:sdf][:Receipt_No])
@hcsy=THcsy.find_by_Sdp_Id(@sdf.Sdp_Id)
end
end
在上述文件的 scan_report 操作中,我创建了两个对象来访问数据库值,并使用这两个对象在下面的模板中显示数据。
view/homes/_hcsy_output_table.html.erb
<div class="block-title">
<div class="block-options">
<a href="javascript:void(0)" class="btn btn-option enable-tooltip" data-toggle="block-collapse" title="Toggle block's content"><i class="fa fa-arrow-up"></i></a>
</div>
<h4>Harischandra Sahayata Yojana Form</h4>
</div>
<!--1st_total_div-->
<div class="block-content">
<div class="block block-themed themed-asphalt">
<div class="block-title"><h5>Personal Details</h5></div>
<div class="block-content full">
<div class="totalaligndiv">
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Receipt No :</div></span>
<input type="text" class="form-control" value= "<%= @sdf.Receipt_No %>">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Date & Time :</div></span>
<input type="text" class="form-control" value= "<%= @hcsy.Created_On %>">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Name of Deceased :</div></span>
<input type="text" class="form-control" placeholder="Name of Deceased">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Mobile No :</div></span>
<input type="text" class="form-control" placeholder="Mobile No">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Beneficiary Name :</div></span>
<input type="text" class="form-control" placeholder="Beneficiary Name">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Occupation :</div></span>
<input type="text" class="form-control" placeholder="Occupation">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Relation with Deceased :</div></span>
<input type="text" class="form-control" placeholder="Relation with Deceased">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Address Line - I :</div></span>
<textarea name="versions comments" placeholder="Address" class="form-control" rows="3"></textarea>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Address Line - II :</div></span>
<textarea name="versions comments" placeholder="Address" class="form-control" rows="3"></textarea>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Village/City :</div></span>
<input type="text" class="form-control" placeholder="Village/City">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Block :</div></span>
<input type="text" class="form-control" placeholder="Block">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">District :</div></span>
<input type="text" class="form-control" placeholder="District">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">State :</div></span>
<input type="text" class="form-control" placeholder="State">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Country :</div></span>
<input type="text" class="form-control" placeholder="Country">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Police Station :</div></span>
<input type="text" class="form-control" placeholder="Police Station">
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="totalaligndiv">
<div class="col-md-6" style="padding-left:0px;">
<div class="block block-themed themed-asphalt">
<div class="block-title"><h5>Annual Income From Sources</h5></div>
<div class="block-content full">
<div class="totalaligndiv">
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">BPL :</div></span>
<input type="text" class="form-control" placeholder="BPL">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Govt. Services :</div></span>
<input type="text" class="form-control" placeholder="Govt. Services">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Business :</div></span>
<input type="text" class="form-control" placeholder="Business">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Land Property :</div></span>
<input type="text" class="form-control" placeholder="Land Property">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Others :</div></span>
<input type="text" class="form-control" placeholder="Others">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left" style="height:55px;">
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
<div class="col-md-6" style="padding-right:0px;">
<div class="block block-themed themed-asphalt">
<div class="block-title"><h5>Application Details</h5></div>
<div class="block-content full">
<div class="totalaligndiv">
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Select Purpose :</div></span>
<input type="text" class="form-control" placeholder="Select Purpose">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Amount Required :</div></span>
<input type="text" class="form-control" placeholder="Amount Required">
</div>
<div>Has he/she received any assistance earlier from this fund</div>
<div class="input-group bmargindiv1 col-md-12 pull-left">
<span class="input-group-addon text-left"><div class="leftsidetextwidth">Status :</div></span>
<input type="text" class="form-control" placeholder="Status">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Prev. Receipt No. :</div></span>
<input type="text" class="form-control" placeholder="Prev. Receipt No.">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Amount Received :</div></span>
<input type="text" class="form-control" placeholder="Amount Received">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Date of Received :</div></span>
<input type="text" class="form-control" placeholder="Date of Received">
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="tpaddingdiv1">
<div class="col-md-4">
<div class="text-center bpaddingdiv1"><img src="image/personsphoto.png" border="0" name="signature" height="140" class="borderblackdiv"></div>
<div class="fontweightbolddiv fontssizediv3 text-center bpaddingdiv1">Deceased Photo</div>
</div>
<div class="col-md-4">
<div class="text-center bpaddingdiv1"><img src="image/personsphoto.png" border="0" name="signature" height="140" class="borderblackdiv"></div>
<div class="fontweightbolddiv fontssizediv3 bpaddingdiv1 text-center">Donor Photo</div>
</div>
<div class="col-md-4">
<div class="text-center bpaddingdiv1"><img src="image/signaturetwo.jpg" border="0" name="signature" height="140" class="borderblackdiv"></div>
<div class="fontweightbolddiv fontssizediv3 bpaddingdiv1 text-center">Donor Thumb</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
检查上面的文件我只能通过@sdf访问值但不能通过@hcsy访问值我need.So请帮助我通过@hcsy对象从数据库获取数据到上面template.My 其他相关文件如下。
scan_report.js.erb:
$("#hcsy_block").html("<%= escape_javascript (render 'hcsy_output_table',locals: "{hcsy: @hcsy }" ) %>");
$("#hcsy_block").slideDown(350);
scan_report.html.erb:
<%= render 'hcsy_output_table',locals: "{hcsy: @hcsy }" %>
_hcsy_output_table.html.erb 将在 div id "hcsy_block" 内的主页上呈现。我只能通过 @sdf 对象访问,但我还需要使用 @hcsy [=33= 的数据] 帮助我通过@hcsy 对象访问数据并让我知道我在哪里做错了。
在上面的主页操作中有此代码:
respond_to do |format|
format.html
format.js
end
您已创建文件 scan_report.js.erb
,但在操作 scan_report
中您没有 js 中的响应代码。那么 AJAX 请求从哪里发送,发送到哪个操作。如果它被发送到 scan_report
操作然后尝试添加这个:
respond_to do |format|
format.js
end
如果这不是解决方案,请添加请求发送到的操作的详细信息以及您发送请求的代码。
我想将两个对象传递给模板,模板将在 Ajax 调用后使用 Rails 3.See 我下面的控制器文件进行渲染。
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
@sdf=TSdf.find_by_Receipt_No(params[:sdf][:Receipt_No])
@hcsy=THcsy.find_by_Sdp_Id(@sdf.Sdp_Id)
end
end
在上述文件的 scan_report 操作中,我创建了两个对象来访问数据库值,并使用这两个对象在下面的模板中显示数据。
view/homes/_hcsy_output_table.html.erb
<div class="block-title">
<div class="block-options">
<a href="javascript:void(0)" class="btn btn-option enable-tooltip" data-toggle="block-collapse" title="Toggle block's content"><i class="fa fa-arrow-up"></i></a>
</div>
<h4>Harischandra Sahayata Yojana Form</h4>
</div>
<!--1st_total_div-->
<div class="block-content">
<div class="block block-themed themed-asphalt">
<div class="block-title"><h5>Personal Details</h5></div>
<div class="block-content full">
<div class="totalaligndiv">
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Receipt No :</div></span>
<input type="text" class="form-control" value= "<%= @sdf.Receipt_No %>">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Date & Time :</div></span>
<input type="text" class="form-control" value= "<%= @hcsy.Created_On %>">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Name of Deceased :</div></span>
<input type="text" class="form-control" placeholder="Name of Deceased">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Mobile No :</div></span>
<input type="text" class="form-control" placeholder="Mobile No">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Beneficiary Name :</div></span>
<input type="text" class="form-control" placeholder="Beneficiary Name">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Occupation :</div></span>
<input type="text" class="form-control" placeholder="Occupation">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Relation with Deceased :</div></span>
<input type="text" class="form-control" placeholder="Relation with Deceased">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Address Line - I :</div></span>
<textarea name="versions comments" placeholder="Address" class="form-control" rows="3"></textarea>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Address Line - II :</div></span>
<textarea name="versions comments" placeholder="Address" class="form-control" rows="3"></textarea>
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Village/City :</div></span>
<input type="text" class="form-control" placeholder="Village/City">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Block :</div></span>
<input type="text" class="form-control" placeholder="Block">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">District :</div></span>
<input type="text" class="form-control" placeholder="District">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">State :</div></span>
<input type="text" class="form-control" placeholder="State">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Country :</div></span>
<input type="text" class="form-control" placeholder="Country">
</div>
<div class="input-group bmargindiv1 col-md-6 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Police Station :</div></span>
<input type="text" class="form-control" placeholder="Police Station">
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="totalaligndiv">
<div class="col-md-6" style="padding-left:0px;">
<div class="block block-themed themed-asphalt">
<div class="block-title"><h5>Annual Income From Sources</h5></div>
<div class="block-content full">
<div class="totalaligndiv">
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">BPL :</div></span>
<input type="text" class="form-control" placeholder="BPL">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Govt. Services :</div></span>
<input type="text" class="form-control" placeholder="Govt. Services">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Business :</div></span>
<input type="text" class="form-control" placeholder="Business">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Land Property :</div></span>
<input type="text" class="form-control" placeholder="Land Property">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Others :</div></span>
<input type="text" class="form-control" placeholder="Others">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left" style="height:55px;">
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
<div class="col-md-6" style="padding-right:0px;">
<div class="block block-themed themed-asphalt">
<div class="block-title"><h5>Application Details</h5></div>
<div class="block-content full">
<div class="totalaligndiv">
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Select Purpose :</div></span>
<input type="text" class="form-control" placeholder="Select Purpose">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Amount Required :</div></span>
<input type="text" class="form-control" placeholder="Amount Required">
</div>
<div>Has he/she received any assistance earlier from this fund</div>
<div class="input-group bmargindiv1 col-md-12 pull-left">
<span class="input-group-addon text-left"><div class="leftsidetextwidth">Status :</div></span>
<input type="text" class="form-control" placeholder="Status">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Prev. Receipt No. :</div></span>
<input type="text" class="form-control" placeholder="Prev. Receipt No.">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Amount Received :</div></span>
<input type="text" class="form-control" placeholder="Amount Received">
</div>
<div class="input-group bmargindiv1 col-md-12 pull-left"> <span class="input-group-addon text-left"><div class="leftsidetextwidth">Date of Received :</div></span>
<input type="text" class="form-control" placeholder="Date of Received">
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="tpaddingdiv1">
<div class="col-md-4">
<div class="text-center bpaddingdiv1"><img src="image/personsphoto.png" border="0" name="signature" height="140" class="borderblackdiv"></div>
<div class="fontweightbolddiv fontssizediv3 text-center bpaddingdiv1">Deceased Photo</div>
</div>
<div class="col-md-4">
<div class="text-center bpaddingdiv1"><img src="image/personsphoto.png" border="0" name="signature" height="140" class="borderblackdiv"></div>
<div class="fontweightbolddiv fontssizediv3 bpaddingdiv1 text-center">Donor Photo</div>
</div>
<div class="col-md-4">
<div class="text-center bpaddingdiv1"><img src="image/signaturetwo.jpg" border="0" name="signature" height="140" class="borderblackdiv"></div>
<div class="fontweightbolddiv fontssizediv3 bpaddingdiv1 text-center">Donor Thumb</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
检查上面的文件我只能通过@sdf访问值但不能通过@hcsy访问值我need.So请帮助我通过@hcsy对象从数据库获取数据到上面template.My 其他相关文件如下。
scan_report.js.erb:
$("#hcsy_block").html("<%= escape_javascript (render 'hcsy_output_table',locals: "{hcsy: @hcsy }" ) %>");
$("#hcsy_block").slideDown(350);
scan_report.html.erb:
<%= render 'hcsy_output_table',locals: "{hcsy: @hcsy }" %>
_hcsy_output_table.html.erb 将在 div id "hcsy_block" 内的主页上呈现。我只能通过 @sdf 对象访问,但我还需要使用 @hcsy [=33= 的数据] 帮助我通过@hcsy 对象访问数据并让我知道我在哪里做错了。
在上面的主页操作中有此代码:
respond_to do |format|
format.html
format.js
end
您已创建文件 scan_report.js.erb
,但在操作 scan_report
中您没有 js 中的响应代码。那么 AJAX 请求从哪里发送,发送到哪个操作。如果它被发送到 scan_report
操作然后尝试添加这个:
respond_to do |format|
format.js
end
如果这不是解决方案,请添加请求发送到的操作的详细信息以及您发送请求的代码。