I am trying to make a ajax call which will return and array of objects and display it on view.. I can display a single object of my class location but not able to iterate through array. I get the following error
ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
My js file is
$("#locations").append("<%= escape_javascript(render(@locations)) %>");
My partial file is _location.erb is
<div id="locations" class="locations">
<% unless @locations.nil %>
<% @locations.each do |location| %>
<%= @locations.name %>
<% end %>
<% end %>
My controller looks like
def search
result = getResultFromSomeWhere
@locations = Array.new(result.size)
result.each do |resultobj|
@locations.push(Location.new(resultobj))
end
respond_to do |format|
format.js
end
end
I think its something to do with serialization as i am passing array of ActiveRecord objects?
Let’s say the size of result is 5, so then
@locations = Array.new(result.size)will return[nil, nil, nil, nil, nil]and then you’re pushing locations to that array
Then you can see your mistake.