我正在尝试使用 Ajax 呈现部分内容,我在其中使用 locals
将相关的 Javascript 对象发送到部分内容。不幸的是,我在发送对象时遇到问题,并且不断收到错误“未定义的局部变量”。
Controller :
class TransportationController < ApplicationController
respond_to :html, :js, :json
创建.js.erb
function getRates(){
$.ajax( {
...
(code cut out that's irrelevant to question, but this request succeeds)
...
success: function( json ) {
json.prices.forEach(function(price){
$("#my_list").append("<%= j render :partial => 'my_item', :locals => {price: price} %>");
});
....
};
_my_item.html.erb:
....
<h1><%= price.display_name %></h1>
....
错误:(NameError - 未定义的局部变量或方法“价格”)
如果能帮助解决这个问题,我们将不胜感激!我以为我做对了
最佳答案
无需在 Ajax 成功 block 中定义 html View ,您可以将 .js 文件与将呈现部分 html 的 Controller 操作相关联。希望它对您有用。
关于javascript - Rails j 局部渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32675372/