Rails j 局部渲染
Rails j render partial with locals
我正在尝试使用 Ajax 渲染部分,我使用 locals
将相关的 Javascript 对象发送到部分。不幸的是,我在发送对象时遇到问题,并且不断收到错误 "undefined local variable".
控制器:
class TransportationController < ApplicationController
respond_to :html, :js, :json
create.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 成功块中定义 html 视图,您可以将 .js 文件与控制器操作相关联,这将呈现部分 html.Hope 它会为你工作。
create.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'");
$("h1.price-display-name").last().text(price.display_name);
});
....
};
_my_item.html.erb: 添加新属性 class
....
<h1 class="price-display-name"></h1>
....
可能是这样
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("<%= escape_javascript render(:partial => 'my_item', :locals => {:price => price}) %>");
});
....
};
您不能将 javascript 变量传递给 rails 助手。
https://www.quora.com/Can-I-pass-a-JavaScript-variable-to-a-Rails-method
http://jing.io/t/pass-javascript-variables-to-rails-controller.html
我正在尝试使用 Ajax 渲染部分,我使用 locals
将相关的 Javascript 对象发送到部分。不幸的是,我在发送对象时遇到问题,并且不断收到错误 "undefined local variable".
控制器:
class TransportationController < ApplicationController
respond_to :html, :js, :json
create.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 成功块中定义 html 视图,您可以将 .js 文件与控制器操作相关联,这将呈现部分 html.Hope 它会为你工作。
create.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'");
$("h1.price-display-name").last().text(price.display_name);
});
....
};
_my_item.html.erb: 添加新属性 class
....
<h1 class="price-display-name"></h1>
....
可能是这样
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("<%= escape_javascript render(:partial => 'my_item', :locals => {:price => price}) %>");
});
....
};
您不能将 javascript 变量传递给 rails 助手。
https://www.quora.com/Can-I-pass-a-JavaScript-variable-to-a-Rails-method
http://jing.io/t/pass-javascript-variables-to-rails-controller.html