Ruby on rails 将局部变量传递到嵌套的局部变量

Ruby on rails 将局部变量传递到嵌套的局部变量,ruby-on-rails,nested,partial-views,Ruby On Rails,Nested,Partial Views,我有一个页面,大致呈现如下集合: index.html.haml = render partial: 'cars_list', as: :this_car, collection: @cars - if car.date == @date %div = car.date _cars_list.html.haml = render partial: 'cars_list', as: :this_car, collection: @cars - if car.date ==

我有一个页面,大致呈现如下集合:

index.html.haml

= render partial: 'cars_list', as: :this_car,  collection: @cars
- if car.date == @date
  %div 
    = car.date
_cars_list.html.haml

= render partial: 'cars_list', as: :this_car,  collection: @cars
- if car.date == @date
  %div 
    = car.date
编辑:\u cars\u列表包含有关单个汽车的其他信息

%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\u stuff.html.haml

= render partial: 'cars_list', as: :this_car,  collection: @cars
- if car.date == @date
  %div 
    = car.date
_cars\u controller.rb

def index
  @cars = Car.all
  @date = params[:date] ? Date.parse(params[:date]) : Date.today
end
日历中发生的事情是,
这辆车始终是cars集合中的第一辆车,即同一日期被反复打印

如果我将
\u calendar\u stuff
中的逻辑移动到
cars\u列表
partial中,则打印结果将按预期更改

因此,Rails似乎没有在每次渲染分部时将本地
这个\u car
对象传递到嵌套分部中

有人知道为什么吗

如果我用

@cars.each do |car|
  render 'cars_list', locals: {this_car: car}
end

我得到了相同的行为。

尝试此重构,看看是否获得了所需的输出:

index.html.haml

= render 'cars_list', collection: @cars, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
%div
  = render 'calendars/calendar_stuff', car: car, date: date
- if car.date == date
  %div 
    = car.date
= render 'cars_list', collection: @cars, as: :this_car, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
  %li this_car.description
%div
  = render 'calendars/calendar_stuff', this_car: this_car, date: date
- if this_car.date == date
  %div 
    = this_car.date
去掉
partial
关键字,并将
@date
实例变量作为局部变量传递,以将逻辑封装到partial中。这一点我是从

\u cars\u list.html.haml

= render 'cars_list', collection: @cars, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
%div
  = render 'calendars/calendar_stuff', car: car, date: date
- if car.date == date
  %div 
    = car.date
= render 'cars_list', collection: @cars, as: :this_car, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
  %li this_car.description
%div
  = render 'calendars/calendar_stuff', this_car: this_car, date: date
- if this_car.date == date
  %div 
    = this_car.date
当您将
@cars
作为
集合
传入时,此部分将引用一个名为
car
的奇异局部变量,然后该变量可以与当前的局部
日期
变量一起传递到下一部分。由于要渲染的部分位于与此处不同的位置(在
calendars/
下),因此此处明确需要
partial
关键字

\u calendar\u stuff.html.haml

= render 'cars_list', collection: @cars, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
%div
  = render 'calendars/calendar_stuff', car: car, date: date
- if car.date == date
  %div 
    = car.date
= render 'cars_list', collection: @cars, as: :this_car, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
  %li this_car.description
%div
  = render 'calendars/calendar_stuff', this_car: this_car, date: date
- if this_car.date == date
  %div 
    = this_car.date
编辑

建议将调用移动到
集合
\u cars\u list.html.haml,但这不适合解决此问题

编辑2

如果您仍然希望将局部变量指定为
This\u car
,则这是上述代码的版本,因此您将覆盖
collection
将自动生成的
car
局部变量

index.html.haml

= render 'cars_list', collection: @cars, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
%div
  = render 'calendars/calendar_stuff', car: car, date: date
- if car.date == date
  %div 
    = car.date
= render 'cars_list', collection: @cars, as: :this_car, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
  %li this_car.description
%div
  = render 'calendars/calendar_stuff', this_car: this_car, date: date
- if this_car.date == date
  %div 
    = this_car.date
\u cars\u list.html.haml

= render 'cars_list', collection: @cars, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
%div
  = render 'calendars/calendar_stuff', car: car, date: date
- if car.date == date
  %div 
    = car.date
= render 'cars_list', collection: @cars, as: :this_car, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
  %li this_car.description
%div
  = render 'calendars/calendar_stuff', this_car: this_car, date: date
- if this_car.date == date
  %div 
    = this_car.date
\u calendar\u stuff.html.haml

= render 'cars_list', collection: @cars, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
%div
  = render 'calendars/calendar_stuff', car: car, date: date
- if car.date == date
  %div 
    = car.date
= render 'cars_list', collection: @cars, as: :this_car, date: @date
%h3 Look at these Cars!
%ul
  %li Something about this car
  %li this_car.description
%div
  = render 'calendars/calendar_stuff', this_car: this_car, date: date
- if this_car.date == date
  %div 
    = this_car.date

尝试过,但仍然有相同的行为。有几件事:1)在
index.html.haml
文件中,
partial:
是必需的,因为我也在使用collection:参数。我相信您只能使用类似于
=渲染“cars\u list”
的方法来结束它。2) 奇异化的局部变量car在
cars\u list.html.haml
文件中工作得很好,但是当我将其传递到
\u calendar\u stuff
中时,它总是在集合中的第一个
car
中传递,而不是迭代器中的当前car。我以前使用过
collection
而没有
partial
关键字,但是如果你发现如果你不使用它会出现问题,一定要继续使用它。我认为这行不通,因为我想在
\u cars\u list
中为每辆车渲染一些代码,然后为同一辆车渲染
\u日历内容。我相信用一个集合来呈现日历的东西会一个接一个地呈现集合中的每个项目的部分。好吧,很公平,那么对集合的调用本来就在正确的位置。因此,我可能会从我的答案中删除该编辑。我想不出为什么只有列表中的第一个对象会被渲染,除非
@cars
只包含一辆车。我的一个应用程序中的代码非常相似,在一个成员对象上呈现一个partial-in-a-partial是没有问题的。我是个白痴。rails代码工作得很好,前端有一个js错误,似乎只传入了集合的第一项。谢谢你的帮助,我将删除这个问题。