Rails j渲染局部与局部。

时间:2022-10-08 17:10:37

I am attempting to render a partial using Ajax, where I send the relevant Javascript object to the partial using locals. Unfortunately, I'm having trouble sending the object and I keep getting the error "undefined local variable".

我正在尝试使用Ajax来呈现一部分,我将相关的Javascript对象发送到局部使用本地。不幸的是,我在发送对象时遇到了麻烦,而且我一直得到“未定义的局部变量”的错误。

controller:

控制器:

class TransportationController < ApplicationController
    respond_to :html, :js, :json 

create.js.erb

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:

_my_item.html.erb:

    ....
        <h1><%= price.display_name %></h1>
    ....

Error: (NameError - undefined local variable or method `price')

错误:(NameError -未定义的局部变量或方法' price')

Any help to sort this out would be greatly appreciated! I thought I was doing it correctly

任何帮助解决这个问题都会非常感谢!我以为我做得对。

4 个解决方案

#1


0  

You can't pass the javascript variable to rails helper.

您不能将javascript变量传递给rails助手。

https://www.quora.com/Can-I-pass-a-JavaScript-variable-to-a-Rails-method

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

http://jing.io/t/pass-javascript-variables-to-rails-controller.html

#2


3  

There is no need to define the html view inside Ajax success block ,you can have your .js file associated with the controller action which will render the partial html.Hope it will work for you.

不需要在Ajax成功块中定义html视图,您可以使用与控制器操作相关联的.js文件来呈现部分html。希望它对你有用。

#3


1  

create.js.erb You can change and add into this:

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: Add new attribute class

_my_item.html。erb:添加新的属性类。

....
<h1 class="price-display-name"></h1>
....

#4


0  

May be this could be the way

可能是这样吗?

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}) %>");
        });
        ....    
};

#1


0  

You can't pass the javascript variable to rails helper.

您不能将javascript变量传递给rails助手。

https://www.quora.com/Can-I-pass-a-JavaScript-variable-to-a-Rails-method

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

http://jing.io/t/pass-javascript-variables-to-rails-controller.html

#2


3  

There is no need to define the html view inside Ajax success block ,you can have your .js file associated with the controller action which will render the partial html.Hope it will work for you.

不需要在Ajax成功块中定义html视图,您可以使用与控制器操作相关联的.js文件来呈现部分html。希望它对你有用。

#3


1  

create.js.erb You can change and add into this:

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: Add new attribute class

_my_item.html。erb:添加新的属性类。

....
<h1 class="price-display-name"></h1>
....

#4


0  

May be this could be the way

可能是这样吗?

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}) %>");
        });
        ....    
};