尝试呈现部分时局部变量始终为零 [英] Local variable always nil when trying to render partial

查看:46
本文介绍了尝试呈现部分时局部变量始终为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试渲染集合时遇到了一个非常奇怪的问题,我什至尝试了不同的方法.

这是我的部分代码(用于调试):

<%= item.inspect -%>

这是我尝试使用它:

<%= render 'item', :collection =>@foo.items %><%= 渲染 'item', :collection =>@foo.items, :as =>:项目%><% @foo.items.each 做 |item|%><%= 渲染 'item', :locals =>{:item =>项目}%><%= 渲染 'item', :object =>项目%><%结束%>

在每个场景中,partial 只输出 nil,但是如果我在每个循环中弹出一个 item.inspect,对象详细信息将按预期显示.

我认为可能有问题的唯一一件事是 items 关联是映射到不同类的短名称,所以我认为 Rails 3 automagic 的事情可能是将它分配给一个变量以匹配该类名,但是如果我尝试输出,我会收到未定义的局部变量错误".

我希望我忽略了一些愚蠢的事情.

解决方案

您是否已经尝试过?—

<% @foo.items.each do |item|%><%= 渲染 'item', :item =>项目%><%结束%>

更新

这是对集合的猜测:

<%= render :partial =>'项目', :collection =>@foo.items, :as =>:项目%>

I'm getting a really strange issue with a partial when trying to render a collection, I've even tried different approaches.

Here is my partial code (for debugging):

<pre><%= item.inspect -%></pre>

And here are my attempts to use it:

<%= render 'item', :collection => @foo.items %>
<%= render 'item', :collection => @foo.items, :as => :item %>

<% @foo.items.each do |item| %>
    <%= render 'item', :locals => {:item => item} %>
    <%= render 'item', :object => item %>
<% end %>

In each of those scenarios the partial just outputs nil, however if I pop a item.inspect inside my each loop the object details are displayed as expected.

The only thing I thought that could be a problem is that the items association is a short name mapped to a different class, so I thought that Rails 3 automagic thing might be assigning it to a variable to match that class name, however if I try and output that I get the 'undefined local variable error'.

I hope I'm overlooking something silly.

解决方案

Have you tried this already? — 

<% @foo.items.each do |item| %>
    <%= render 'item', :item => item %>
<% end %>

Update

Here's a guess for the collection:

<%= render :partial => 'item', :collection => @foo.items, :as => :item  %>

这篇关于尝试呈现部分时局部变量始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆