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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With