Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 336
tomtom Avatar asked Nov 20 '25 09:11

tomtom


1 Answers

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.

like image 159
rakeshpatra Avatar answered Nov 21 '25 22:11

rakeshpatra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!