vendredi 25 septembre 2015

Rails j render partial with locals

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".

controller:

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>
    ....

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

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

Aucun commentaire:

Enregistrer un commentaire