I have a page that renders a collection roughly like this:
index.html.haml
= render partial: 'cars_list', as: :this_car, collection: @cars
_cars_list.html.haml
Edit: _cars_list has other info about the individual car.
%h3 Look at these Cars!
%ul
%li Something about this car
%li car.description
%div
= render partial: 'calendars/calendar_stuff', locals: {car: this_car}
_calendar_stuff.html.haml
- if car.date == @date
%div
= car.date
_cars_contoller.rb
def index
@cars = Car.all
@date = params[:date] ? Date.parse(params[:date]) : Date.today
end
What happens in the calendar stuff partial is that this_car
is always the first car in the cars collection, i.e. the same date gets printed over and over.
If I move the logic in _calendar_stuff
into the cars_list
partial, then the printed result changes as expected.
So it seems that Rails is not passing the local this_car
object into the nested partial each time it renders the partial.
Does anyone know why?
P.S. If I structure the code with
@cars.each do |car|
render 'cars_list', locals: {this_car: car}
end
I get the same behavior.