在Rails中渲染部分时,'locals'可以与'collection'一起使用吗?

Gav*_*Gav 16 ruby-on-rails

当我尝试渲染这样的部分时,一切正常:

= render :partial => "/shared/enquiry/car_type", :collection => @enquiry.available_car_types
Run Code Online (Sandbox Code Playgroud)

但是,如果我还想传递一个变量(在这种情况下是'path',因为我在两个表单中共享这个部分),我无法使用该路径:

= render :partial => "/shared/enquiry/car_type", :collection => @enquiry.available_car_types, :locals => {:path => customers_enquiry_path}
Run Code Online (Sandbox Code Playgroud)

我尝试过移动的东西,但似乎没有任何工作,让我相信一个人不能使用收藏品的本地人.任何帮助,将不胜感激.

GAV

xgu*_*uox 26

顺便说一下,对我来说,就像导游说的那样,:as在调用部分时指定选项

在此输入图像描述


小智 18

对于Rails 4.x,如果将集合直接传递给渲染(如果要对异构集合使用自动部分选择,则必须执行此操作),下一个参数将被解释为本地散列.

请尝试以下方法:

= render @enquiry.available_car_types, :path => customers_enquiry_path
Run Code Online (Sandbox Code Playgroud)


小智 12

你用的是什么版本?使用我的2.3.5我能做到这一点:

render :partial => "/site_articles/article", :collection => @site_articles, :locals => { :footer => true }
Run Code Online (Sandbox Code Playgroud)

,你可以在其他地方找到解释,如3.4.6 .


Sal*_*lil -10

不,你不能一起使用它们。参考这个

我认为你必须做类似以下的事情

  <% for ad in @enquiry.available_car_types %>
    <%= render :partial => "/shared/enquiry/car_type", :locals =>{ :ad => ad, :path =>customers_enquiry_path } %>
  <% end %>
Run Code Online (Sandbox Code Playgroud)

  • 我将强调,可以将它们一起使用。我已经多次使用过这种方法。 (4认同)